How can I generate a cube using JavaScript and this coordinate format?

Given this code:

{
( 256 64 16 ) ( 256 64 0 ) ( 256 0 16 ) mmetal1_2 0 0 0 1 1
( 0 0 0 ) ( 0 64 0 ) ( 0 0 16 ) mmetal1_2 0 0 0 1 1
( 64 256 16 ) ( 0 256 16 ) ( 64 256 0 ) mmetal1_2 0 0 0 1 1
( 0 0 0 ) ( 0 0 16 ) ( 64 0 0 ) mmetal1_2 0 0 0 1 1
( 64 64 0 ) ( 64 0 0 ) ( 0 64 0 ) mmetal1_2 0 0 0 1 1
( 0 0 -64 ) ( 64 0 -64 ) ( 0 64 -64 ) mmetal1_2 0 0 0 1 1
}

      

How can I generate a cube using JavaScript (or any additional library) and the coordinates above? The above code should be interpreted as follows:

In the example shown here, this brush is a hexagonal cube. The plane of his first person is defined by three points (256 64 16) (256 64 0) (256 0 16). Other information provided is the texture used by the face. "mmetal1_2" is the name of the texture, only one plane can have a single texture. "0 0 0 1 1" is a texture mapping and, accordingly, "X offset" "Y offset" "rotation" "X scale" and "Y scale".

Plane points (p1) (p2) (p3) are interpreted as follows. plane points should be located in such a way that the cross product vectors (p3 - p1) and (p2 - p1) are not zero, i.e. three points must be linearly independent. The normalized cross product is then the normal vector of the plane. Each point p for which (p - p1) * normal & lt = 0 (where * is the dot product) is considered to be in the half-space defined by the plane. Every other point is considered to be not in the half-space.

Note # 1: CSS can be used if needed.

Note # 2: I really need to understand the concept and math functions. I can extract the coordinates using a JavaScript loop, but I don't know how to initially approach this. I just need to nudge in the right direction.

Note # 3: I only want the first three sets of values, not texture and offset.

Here are the specifications for this coordinate system: https://quakewiki.org/wiki/Quake_Map_Format

+3


source to share


1 answer


Points (p1)(p2)(p3)

define a plane in terms of a triangle on the plane. The points are located so that the normal of the plane (normalized "cross product of vectors (p3 - p1) and (p2 - p1)") points outward. We can define a plane through a flat equation Ax+Bx+Cx+D=0

as follows:

   (A, B, C) = N = normalize(cross(p3-p1,p2-p1))
   D = -dot(p1,N)

      

The intersection of these planes forms a convex polyhedron. Finding this polyhedron involves finding the vertices of the intersection of the planes as one of the steps. The article you mention the wiki link to an article of the magazine explains how to generate these vertices.

The format you specify describes a convex polyhedron (convex polyhedron) using its half-space (or h-representation or h-rep). Since a given set of planes can describe many convex polytopes, most likely you want to transform the minimal half-space of a convex polytope representation to its minimal vertex representation (or v-representation or v-rep). Here the minimal representation is one whose vertices intersect at least three planes, but describe a rigid body that intersects all half-spaces. Then you need to generate the convex hull of the minimum vertex representation.




Since the question asks for more detail, I'll add it:

Generating a polyhedron mesh includes the following steps.

  • For each set of flat points, (p1)(p2)(p3)

    find the coefficients of the flat equation as defined above.
  • Find a set of points that intersect three or more planes. This is done by checking if each set of three planes intersect at a point. The result will usually be much larger than the vertices in the final polyhedron, so now we need to discard them.
  • For each point, check if that point is inside all planes. More precisely, a point is inside a plane if D+dot(P,N) <= 0

    , where D and N are plane parameters, as indicated above, and P is a point of view. Only those points that are inside all planes are saved.
  • Eliminate duplicate points. Usually, duplicates indicate that more than three planes intersect at the same point.
  • The last step is to create a convex hull of points. Algorithms like QuickHull can be helpful here. The result will be a convex polyhedron with each face being a convex polygon. If necessary, each face of the polyhedron can be triangulated using a relatively trivial procedure.

I have written some code that implements this method.

+5


source







All Articles