/// Compute polygon centroids based on point position attributes /// Results are stored in internally stored attribute map named "polygon_centroids". public void ComputePolygonCentroids() { var polygonCentroids = PolygonAttributes.FindOrCreate <Vector3>("polygon_centroids"); var pointLocations = PointAttributes.Find <Vector3>("point_locations"); polygonCentroids.Clear(); foreach (Polygon polygon in Polygons) { polygon.ComputeCentroid(polygonCentroids, pointLocations); } }
/// Compute polygon normals based on point position attributes. /// Results are stored in internally stored attribute map named "polygon_normals". public void ComputePolygonNormals() { var polygonNormals = PolygonAttributes.FindOrCreate <Vector3>("polygon_normals"); var pointLocations = PointAttributes.Find <Vector3>("point_locations"); polygonNormals.Clear(); foreach (Polygon polygon in Polygons) { polygon.ComputeNormal(polygonNormals, pointLocations); } }
public SuperEllipsoid(Vector3 radius, double n1, double n2, int sliceCount, int stackDivision) { MakeInfo info = new MakeInfo(radius, n1, n2, sliceCount, stackDivision); info.pointLocations = PointAttributes.FindOrCreate <Vector3>("point_locations"); info.pointNormals = PointAttributes.FindOrCreate <Vector3>("point_normals"); info.pointTexcoords = PointAttributes.FindOrCreate <Vector2>("point_texcoords"); info.polygonCentroids = PolygonAttributes.FindOrCreate <Vector3>("polygon_centroids"); info.polygonNormals = PolygonAttributes.FindOrCreate <Vector3>("polygon_normals"); info.cornerTexcoords = CornerAttributes.FindOrCreate <Vector2>("corner_texcoords"); int slice; int stack; for (slice = 0; slice < sliceCount; ++slice) { double relSlice = (double)(slice) / (double)(sliceCount); for (stack = -stackDivision; stack <= stackDivision; ++stack) { double relStack = (double)(stack) / (double)(stackDivision + 1); Point point = MakePoint(info, relSlice, relStack); info.points.Add(point); } } info.bottom = MakePoint(info, 0.5f, -1.0f); info.top = MakePoint(info, 0.5f, 1.0f); #region bottom fan { for (slice = 0; slice < sliceCount; ++slice) { int nextSlice = (slice + 1); int stackBase0 = 0; double relSlice = ((double)(slice) + 0.5) / (double)(sliceCount); double relStack = -1.0 + (0.5 / (double)(stackDivision + 1)); Point centroid = MakePoint(info, relSlice, relStack); var polygon = MakePolygon(); #if CCW MakeCorner(info, polygon, slice, stackBase0); MakeCorner(info, polygon, slice, info.stackBase0Bottom); MakeCorner(info, polygon, nextSlice, stackBase0); #else MakeCorner(info, polygon, nextSlice, stackBase0); MakeCorner(info, polygon, slice, info.stackBase0Bottom); MakeCorner(info, polygon, slice, stackBase0); #endif info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } } #endregion #region middle quads, bottom up for ( stack = -stackDivision; stack < stackDivision; ++stack ) { int stackBase0 = stack + stackDivision; int nextStackBase0 = stackBase0 + 1; double relStack = ((double)(stack) + 0.5) / (double)(stackDivision + 1); for (slice = 0; slice < sliceCount; ++slice) { int nextSlice = (slice + 1); double relSlice = ((double)(slice) + 0.5) / (double)(sliceCount); Point centroid = MakePoint(info, relSlice, relStack); var polygon = MakePolygon(); #if CCW MakeCorner(info, polygon, nextSlice, nextStackBase0); MakeCorner(info, polygon, slice, nextStackBase0); MakeCorner(info, polygon, slice, stackBase0); MakeCorner(info, polygon, nextSlice, stackBase0); #else MakeCorner(info, polygon, nextSlice, stackBase0); MakeCorner(info, polygon, slice, stackBase0); MakeCorner(info, polygon, slice, nextStackBase0); MakeCorner(info, polygon, nextSlice, nextStackBase0); #endif info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } } #endregion #region top fan for (slice = 0; slice < sliceCount; ++slice) { int nextSlice = (slice + 1); int stackBase0 = stackDivision + stackDivision; double relSlice = ((double)(slice) + 0.5) / (double)(sliceCount); double relStack = 1.0 - (0.5 / (double)(stackDivision + 1)); Point centroid = MakePoint(info, relSlice, relStack); var polygon = MakePolygon(); #if CCW MakeCorner(info, polygon, slice, info.stackBase0Top); MakeCorner(info, polygon, slice, stackBase0); MakeCorner(info, polygon, nextSlice, stackBase0); #else MakeCorner(info, polygon, nextSlice, stackBase0); MakeCorner(info, polygon, slice, stackBase0); MakeCorner(info, polygon, slice, info.stackBase0Top); #endif info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } #endregion }
public Disc( double outerRadius, double innerRadius, int sliceCount, int stackCount ) { MakeInfo info = new MakeInfo(outerRadius, innerRadius, sliceCount, stackCount); info.pointLocations = PointAttributes.FindOrCreate <Vector3>("point_locations"); info.pointNormals = PointAttributes.FindOrCreate <Vector3>("point_normals"); info.pointTexcoords = PointAttributes.FindOrCreate <Vector2>("point_texcoords"); info.polygonCentroids = PolygonAttributes.FindOrCreate <Vector3>("polygon_centroids"); info.polygonNormals = PolygonAttributes.FindOrCreate <Vector3>("polygon_normals"); info.cornerNormals = CornerAttributes.FindOrCreate <Vector3>("corner_normals"); info.cornerTexcoords = CornerAttributes.FindOrCreate <Vector2>("corner_texcoords"); /* Make points */ for (int stack = 0; stack < stackCount; ++stack) { double relStack = (stackCount == 1) ? 1.0 : (double)stack / (double)(stackCount - 1); for (int slice = 0; slice <= sliceCount; ++slice) { double relSlice = (double)slice / (double)sliceCount; info.points[new KeyValuePair <int, int>(slice, stack)] = MakePoint(info, relSlice, relStack); } } /* Special case without center point */ if (stackCount == 1) { Polygon polygon = MakePolygon(); info.polygonCentroids[polygon] = Vector3.Zero; info.polygonNormals [polygon] = Vector3.UnitZ; for (int slice = sliceCount - 1; slice >= 0; --slice) { MakeCorner(info, polygon, slice, 0); } return; } /* Make center point if needed */ if (innerRadius == 0.0f) { info.center = MakePoint(0.0, 0.0, 0.0); } /* Quads/triangles */ for (int stack = 0; stack < stackCount - 1; ++stack) { double relStackCentroid = (stackCount == 1) ? 0.5 : (double)stack / (double)(stackCount - 1); for (int slice = 0; slice < sliceCount; ++slice) { double relSliceCentroid = ((double)(slice) + 0.5) / (double)(sliceCount); Point centroid = MakePoint(info, relSliceCentroid, relStackCentroid); Polygon polygon = MakePolygon(); info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = Vector3.UnitZ; if ((stack == 0) && (innerRadius == 0.0)) { Corner tip = MakeCorner(info, polygon, slice, stack); MakeCorner(info, polygon, slice + 1, stack + 1); MakeCorner(info, polygon, slice, stack + 1); Vector2 t1 = info.pointTexcoords[GetPoint(info, slice, stack)]; Vector2 t2 = info.pointTexcoords[GetPoint(info, slice + 1, stack)]; Vector2 averageTexcoord = (t1 + t2) / 2.0f; info.cornerTexcoords[tip] = averageTexcoord; } else { MakeCorner(info, polygon, slice + 1, stack + 1); MakeCorner(info, polygon, slice, stack + 1); MakeCorner(info, polygon, slice, stack); MakeCorner(info, polygon, slice + 1, stack); } } } BuildEdges(); }
public Cone( double minX, double maxX, double bottomRadius, double topRadius, bool useBottom, bool useTop, int sliceCount, int stackDivision ) { MakeInfo info = new MakeInfo(minX, maxX, bottomRadius, topRadius, useBottom, useTop, sliceCount, stackDivision); info.pointLocations = PointAttributes.FindOrCreate <Vector3>("point_locations"); info.pointNormals = PointAttributes.FindOrCreate <Vector3>("point_normals"); info.pointTexcoords = PointAttributes.FindOrCreate <Vector2>("point_texcoords"); info.polygonCentroids = PolygonAttributes.FindOrCreate <Vector3>("polygon_centroids"); info.polygonNormals = PolygonAttributes.FindOrCreate <Vector3>("polygon_normals"); info.cornerNormals = CornerAttributes.FindOrCreate <Vector3>("corner_normals"); info.cornerTexcoords = CornerAttributes.FindOrCreate <Vector2>("corner_texcoords"); /* Other vertices */ for (int slice = 0; slice <= sliceCount; ++slice) { double relSlice = (double)slice / (double)sliceCount; for (int stack = -stackDivision - info.bottomNotSingular; stack <= stackDivision + info.topNotSingular; ++stack) { double relStack = (double)stack / (double)(stackDivision + 1); info.points[new KeyValuePair <int, int>(slice, stack)] = ConePoint(info, relSlice, relStack); } } /* Bottom parts */ if (bottomRadius == 0.0) { info.bottom = MakePoint(minX, 0.0, 0.0); /* apex */ for (int slice = 0; slice < sliceCount; ++slice) { int stack = -stackDivision; // second last stack, bottom is -stackDivision - 1 double relSliceCentroid = ((double)slice + 0.5) / (double)sliceCount; double relStackCentroid = -1.0 + (0.5 / (double)(stackDivision + 1)); Point centroid = ConePoint(info, relSliceCentroid, relStackCentroid); Polygon polygon = MakePolygon(); MakeCorner(info, polygon, slice + 1, stack); MakeCorner(info, polygon, slice, stack); Corner tip = MakeCorner(info, polygon, slice, -stackDivision - 1); Vector3 n1 = info.pointNormals[GetPoint(info, slice, stack)]; Vector3 n2 = info.pointNormals[GetPoint(info, slice + 1, stack)]; Vector3 averageNormal = Vector3.Normalize(n1 + n2); info.cornerNormals[tip] = averageNormal; Vector2 t1 = info.pointTexcoords[GetPoint(info, slice, stack)]; Vector2 t2 = info.pointTexcoords[GetPoint(info, slice + 1, stack)]; Vector2 averageTexCoord = (t1 + t2) / 2.0f; info.cornerTexcoords[tip] = averageTexCoord; info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } } else { if (useBottom == true) { Polygon polygon = MakePolygon(); info.polygonCentroids[polygon] = new Vector3((float)minX, 0.0f, 0.0f); info.polygonNormals [polygon] = new Vector3(-1.0f, 0.0f, 0.0f); for (int slice = 0; slice < sliceCount; ++slice) { int reverseSlice = sliceCount - 1 - slice; MakeCorner(info, polygon, reverseSlice, -stackDivision - 1, true); } } } /* Middle quads, bottom up */ for ( int stack = -stackDivision - info.bottomNotSingular; stack < stackDivision + info.topNotSingular; ++stack ) { double relStackCentroid = ((double)stack + 0.5) / (double)(stackDivision + 1); for (int slice = 0; slice < sliceCount; ++slice) { double relSliceCentroid = ((double)(slice) + 0.5) / (double)(sliceCount); Point centroid = ConePoint(info, relSliceCentroid, relStackCentroid); Polygon polygon = MakePolygon(); MakeCorner(info, polygon, slice + 1, stack + 1); MakeCorner(info, polygon, slice, stack + 1); MakeCorner(info, polygon, slice, stack); MakeCorner(info, polygon, slice + 1, stack); info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } } /* Top parts */ if (topRadius == 0.0) { info.top = MakePoint(maxX, 0.0, 0.0); /* apex */ for (int slice = 0; slice < sliceCount; ++slice) { int stack = stackDivision; double relSliceCentroid = ((double)(slice) + 0.5) / (double)(sliceCount); double relStackCentroid = 1.0 - (0.5 / (double)(stackDivision + 1)); Point centroid = ConePoint(info, relSliceCentroid, relStackCentroid); Polygon polygon = MakePolygon(); Corner tip = MakeCorner(info, polygon, slice, stackDivision + 1); MakeCorner(info, polygon, slice, stack); MakeCorner(info, polygon, slice + 1, stack); Vector3 n1 = info.pointNormals[GetPoint(info, slice, stack)]; Vector3 n2 = info.pointNormals[GetPoint(info, slice + 1, stack)]; Vector3 averageNormal = Vector3.Normalize(n1 + n2); info.cornerNormals[tip] = averageNormal; Vector2 t1 = info.pointTexcoords[GetPoint(info, slice, stack)]; Vector2 t2 = info.pointTexcoords[GetPoint(info, slice + 1, stack)]; Vector2 averageTexcoord = (t1 + t2) / 2.0f; info.cornerTexcoords[tip] = averageTexcoord; info.polygonCentroids[polygon] = info.pointLocations[centroid]; info.polygonNormals [polygon] = info.pointNormals [centroid]; } } else { if (useTop == true) { Polygon polygon = MakePolygon(); info.polygonCentroids[polygon] = new Vector3((float)maxX, 0.0f, 0.0f); info.polygonNormals [polygon] = new Vector3(1.0f, 0.0f, 0.0f); for (int slice = 0; slice < sliceCount; ++slice) { MakeCorner(info, polygon, slice, stackDivision + 1, true); } } } //MergePoints(); }
public Cube(Vector3 size, IVector3 div, float p) { int x; int y; int z; MakeInfo info = new MakeInfo(0.5f * size, div, p); info.pointLocations = PointAttributes.FindOrCreate <Vector3>("point_locations"); info.pointNormals = PointAttributes.FindOrCreate <Vector3>("point_normals"); info.pointTexcoords = PointAttributes.FindOrCreate <Vector2>("point_texcoords"); info.cornerNormals = CornerAttributes.FindOrCreate <Vector3>("corner_normals"); info.cornerTexcoords = CornerAttributes.FindOrCreate <Vector2>("corner_texcoords"); info.polygonCentroids = PolygonAttributes.FindOrCreate <Vector3>("polygon_centroids"); info.polygonNormals = PolygonAttributes.FindOrCreate <Vector3>("polygon_normals"); // Generate vertices // Top and bottom for (x = -info.Div.X; x <= info.Div.X; x++) { float relX = 0.5f + (float)x / info.Size.X; for (z = -info.Div.Z; z <= info.Div.Z; z++) { float relZ = 0.5f + (float)z / info.Size.Z; MakePoint(info, x, info.Div.Y, z, Vector3.UnitY, relX, relZ); MakePoint(info, x, -info.Div.Y, z, -Vector3.UnitY, relX, relZ); } for (y = -info.Div.Y; y <= info.Div.Y; y++) { float relY = 0.5f + (float)y / info.Size.Y; MakePoint(info, x, y, info.Div.Z, Vector3.UnitZ, relX, relY); MakePoint(info, x, y, -info.Div.Z, -Vector3.UnitZ, relX, relY); } } // Left and right for (z = -info.Div.Z; z <= info.Div.Z; z++) { float relZ = 0.5f + (float)z / info.Size.Z; for (y = -info.Div.Y; y <= info.Div.Y; y++) { float relY = 0.5f + (float)y / info.Size.Y; MakePoint(info, info.Div.X, y, z, Vector3.UnitX, relY, relZ); MakePoint(info, -info.Div.X, y, z, -Vector3.UnitX, relY, relZ); } } // Generate quads // Top and bottom for (x = -info.Div.X; x < info.Div.X; x++) { float relX1 = 0.5f + (float)x / info.Size.X; float relX2 = 0.5f + (float)(x + 1) / info.Size.X; for (z = -info.Div.Z; z < info.Div.Z; z++) { float relZ1 = 0.5f + (float)z / info.Size.Z; float relZ2 = 0.5f + (float)(z + 1) / info.Size.Z; var top = MakePolygon(); MakeCorner(info, top, x + 1, info.Div.Y, z, Vector3.UnitY, relX2, relZ1); MakeCorner(info, top, x + 1, info.Div.Y, z + 1, Vector3.UnitY, relX2, relZ2); MakeCorner(info, top, x, info.Div.Y, z + 1, Vector3.UnitY, relX1, relZ2); MakeCorner(info, top, x, info.Div.Y, z, Vector3.UnitY, relX1, relZ1); var bottom = MakePolygon(); MakeCorner(info, bottom, x, -info.Div.Y, z, -Vector3.UnitY, relX1, relZ1); MakeCorner(info, bottom, x, -info.Div.Y, z + 1, -Vector3.UnitY, relX1, relZ2); MakeCorner(info, bottom, x + 1, -info.Div.Y, z + 1, -Vector3.UnitY, relX2, relZ2); MakeCorner(info, bottom, x + 1, -info.Div.Y, z, -Vector3.UnitY, relX2, relZ1); info.polygonNormals[top] = Vector3.UnitY; info.polygonNormals[bottom] = -Vector3.UnitY; } for (y = -info.Div.Y; y < info.Div.Y; y++) { float relY1 = 0.5f + (float)y / info.Size.Y; float relY2 = 0.5f + (float)(y + 1) / info.Size.Y; var back = MakePolygon(); MakeCorner(info, back, x, y, info.Div.Z, Vector3.UnitZ, relX1, relY1); MakeCorner(info, back, x, y + 1, info.Div.Z, Vector3.UnitZ, relX1, relY2); MakeCorner(info, back, x + 1, y + 1, info.Div.Z, Vector3.UnitZ, relX2, relY2); MakeCorner(info, back, x + 1, y, info.Div.Z, Vector3.UnitZ, relX2, relY1); var front = MakePolygon(); MakeCorner(info, front, x + 1, y, -info.Div.Z, -Vector3.UnitZ, relX2, relY1); MakeCorner(info, front, x + 1, y + 1, -info.Div.Z, -Vector3.UnitZ, relX2, relY2); MakeCorner(info, front, x, y + 1, -info.Div.Z, -Vector3.UnitZ, relX1, relY2); MakeCorner(info, front, x, y, -info.Div.Z, -Vector3.UnitZ, relX1, relY1); info.polygonNormals[back] = Vector3.UnitZ; info.polygonNormals[front] = -Vector3.UnitZ; } } // Left and right for (z = -info.Div.Z; z < info.Div.Z; z++) { float relZ1 = 0.5f + (float)z / info.Size.Z; float relZ2 = 0.5f + (float)(z + 1) / info.Size.Z; for (y = -info.Div.Y; y < info.Div.Y; y++) { float relY1 = 0.5f + (float)y / info.Size.Y; float relY2 = 0.5f + (float)(y + 1) / info.Size.Y; var right = MakePolygon(); MakeCorner(info, right, info.Div.X, y, z, Vector3.UnitX, relY1, relZ1); MakeCorner(info, right, info.Div.X, y, z + 1, Vector3.UnitX, relY1, relZ2); MakeCorner(info, right, info.Div.X, y + 1, z + 1, Vector3.UnitX, relY2, relZ2); MakeCorner(info, right, info.Div.X, y + 1, z, Vector3.UnitX, relY2, relZ1); var left = MakePolygon(); MakeCorner(info, left, -info.Div.X, y + 1, z, -Vector3.UnitX, relY2, relZ1); MakeCorner(info, left, -info.Div.X, y + 1, z + 1, -Vector3.UnitX, relY2, relZ2); MakeCorner(info, left, -info.Div.X, y, z + 1, -Vector3.UnitX, relY1, relZ2); MakeCorner(info, left, -info.Div.X, y, z, -Vector3.UnitX, relY1, relZ1); info.polygonNormals[right] = Vector3.UnitX; info.polygonNormals[left] = -Vector3.UnitX; } } ComputePolygonCentroids(); }