Skip to content

Commit

Permalink
Migrate Revolve
Browse files Browse the repository at this point in the history
  • Loading branch information
John Collins committed Nov 30, 2023
1 parent 293772a commit d0bfb36
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 103 deletions.
94 changes: 0 additions & 94 deletions bindings/java/src/main/cpp/manifold3d/mesh_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,98 +183,4 @@ manifold::Manifold Loft(const manifold::CrossSection section, const std::vector<
return Loft(sections, transforms);
}

manifold::Manifold Revolve(const manifold::CrossSection& crossSection,
int circularSegments,
float revolveDegrees = 360.0f) {

manifold::Rect bounds = crossSection.Bounds();
manifold::Polygons polygons;

// Take the x>=0 slice.
if (bounds.min.x < 0) {
glm::vec2 min = bounds.min;
glm::vec2 max = bounds.max;
manifold::CrossSection posBoundingBox = manifold::CrossSection({{0.0, min.y},{max.x, min.y},
{max.x,max.y},{0.0,max.y}});

// Can't use RectClip unfortunately as it has many failure cases.
polygons = (crossSection ^ posBoundingBox).ToPolygons();
} else {
polygons = crossSection.ToPolygons();
}

float radius = 0.0f;
for (const auto& poly : polygons) {
for (const auto& vert : poly) {
radius = fmax(radius, vert.x);
}
}

bool isFullRevolution = revolveDegrees >= 360.0f;

int nDivisions = circularSegments > 2 ? circularSegments
: manifold::Quality::GetCircularSegments(radius);

std::vector<glm::vec3> vertPos;
std::vector<glm::ivec3> triVerts;

std::vector<int> startPoses;
std::vector<int> endPoses;

float dPhi = revolveDegrees / nDivisions;

for (const auto& poly : polygons) {
for (std::size_t polyVert = 0; polyVert < poly.size(); ++polyVert) {

int startVert = vertPos.size();

if (!isFullRevolution)
startPoses.push_back(startVert);

// first and last slice are distinguished if not a full revolution.
int nSlices = isFullRevolution ? nDivisions : nDivisions + 1;
int lastStart = startVert + (polyVert == 0 ? nSlices * (poly.size() - 1) : -nSlices);

for (int slice = 0; slice < nSlices; ++slice) {

float phi = slice * dPhi;
glm::vec2 pos = poly[polyVert];
glm::vec3 p = {pos.x * manifold::cosd(phi), pos.x * manifold::sind(phi), pos.y};
vertPos.push_back(p);

int lastSlice = (slice == 0 ? nDivisions : slice) - 1;
if (isFullRevolution || slice > 0) {
triVerts.push_back({startVert + slice, startVert + lastSlice,
lastStart + lastSlice});
triVerts.push_back(
{lastStart + lastSlice, lastStart + slice, startVert + slice});
}

}
if (!isFullRevolution)
endPoses.push_back(vertPos.size() -1);
}
}

// Add front and back triangles if not a full revolution.
if (!isFullRevolution ) {
std::vector<glm::ivec3> frontTriangles = manifold::Triangulate(polygons, 0.0001);
for (auto& tv: frontTriangles) {
glm::vec3 t = {startPoses[tv.x], startPoses[tv.y], startPoses[tv.z]};
triVerts.push_back(t);
}

for (auto& v: frontTriangles) {
glm::vec3 t = {endPoses[v.z], endPoses[v.y], endPoses[v.x]};
triVerts.push_back(t);
}
}

manifold::Mesh mesh;
mesh.vertPos = vertPos;
mesh.triVerts = triVerts;
return manifold::Manifold(mesh);
}


}
9 changes: 3 additions & 6 deletions bindings/java/src/main/java/manifold3d/Manifold.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,8 @@ public Manifold scale(double x, double y, double z) {

public static native @ByVal Manifold Sphere(float radius, int circularSegments);
public static native @ByVal Manifold Extrude(@ByRef CrossSection crossSection, float height, int nDivisions, float twistDegrees, @ByRef DoubleVec2 scaleTop);
public static @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments) {
return MeshUtils.Revolve(crossSection, circularSegments);
}
public static @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments, float revolveDegrees) {
return MeshUtils.Revolve(crossSection, circularSegments, revolveDegrees);
}

public static native @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments);
public static native @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments, float revolveDegrees);
public static native @ByVal Manifold Compose(@ByRef ManifoldVector manifolds);
}
3 changes: 0 additions & 3 deletions bindings/java/src/main/java/manifold3d/MeshUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,4 @@ public static Manifold PolyhedronFromBuffers(DoubleBuffer vertices, long nVertic

public static native @ByVal Manifold Loft(@ByRef CrossSectionVector sections, @ByRef DoubleMat4x3Vector transforms);
public static native @ByVal Manifold Loft(@ByRef CrossSection section, @ByRef DoubleMat4x3Vector transforms);

public static native @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments);
public static native @ByVal Manifold Revolve(@ByRef CrossSection crossSection, int circularSegments, float revolveDegrees);
}

0 comments on commit d0bfb36

Please sign in to comment.