Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plane clipping is too inaccurate (sorry best description) #44

Open
JSandusky opened this issue Dec 29, 2016 · 0 comments
Open

Plane clipping is too inaccurate (sorry best description) #44

JSandusky opened this issue Dec 29, 2016 · 0 comments

Comments

@JSandusky
Copy link

Given this function where "triangle_" is the library's triangle, the split results are radically different and not even sane. The only sanity they have is positional based on the objects involved.

The "side markings" and all other data are completely unused in the sanity checks, and those are all that I'm worried about. This code should not result in dead zones of zero area polygons (checked against LibGDX in java for a throwaway), but it does result in void spaces when used with the library.

Far too often splitting a triangle by a plane results in a split that does not make sense when flipping the plane. A split of 2 followed by a split of 2, or a split of 1 followed by a split of 1. That involves this library because those splits come from this library's planar functions and triangle functions.

void MeshMerger::SplitTriangles(std::vector<CSGTriangle>& lhs, std::vector<CSGTriangle>& rhs)
    {
        for (unsigned leftIndex = 0; leftIndex < lhs.size(); ++leftIndex)
        {
            CSGTriangle& current = lhs[leftIndex];
            for (unsigned j = 0; j < rhs.size(); ++j)
            {
                // Don't allow a polygon to split a product more than once, triangle-to-triangle test is false alarm
                if (rhs[j].lastSplitter_ == leftIndex)
                    continue;
                if (!current.triangle_.Intersects(rhs[j].triangle_))
                    continue;
                Triangle a0, b0;
                Triangle a1, b1;
                Plane splitPlane = current.triangle_.PlaneCCW();
                int ret = splitPlane.Clip(rhs[j].triangle_, a0, b0);
                if (ret > 0)
                {
                    CSGTriangle aa0;
                    aa0.triangle_ = a0;
                    aa0.lastSplitter_ = leftIndex;
                    aa0.backSideMarked_ = 1;
                    rhs.push_back(aa0);
                    if (ret == 2)
                    {
                        CSGTriangle bb0;
                        bb0.backSideMarked_ = 1;
                        bb0.lastSplitter_ = leftIndex;
                        bb0.triangle_ = b0;
                        rhs.push_back(bb0);
                    }

                    splitPlane.ReverseNormal();
                    ret = splitPlane.Clip(rhs[j].triangle_, a1, b1);
                    if (ret > 0)
                    {
                        CSGTriangle aa1;
                        aa1.triangle_ = a1;
                        aa1.backSideMarked_ = -1;
                        aa1.lastSplitter_ = leftIndex;
                        rhs.push_back(aa1);
                        if (ret == 2)
                        {
                            CSGTriangle bb1;
                            bb1.backSideMarked_ = -1;
                            bb1.triangle_ = b1;
                            bb1.lastSplitter_ = leftIndex;
                            rhs.push_back(bb1);
                        }
                    }

                    // Remove the triangle that we have split
                    rhs.erase(rhs.begin() + j);
                    --j;
                }
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant