Skip to content

Commit

Permalink
new adjust-edge-length-extended is optionally available.
Browse files Browse the repository at this point in the history
  • Loading branch information
miho committed Jul 19, 2017
1 parent 14ad7cc commit 1da2c41
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 55 deletions.
2 changes: 1 addition & 1 deletion gradle/project-info.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// -----------------------------------------------------------------------------
ext.publishing.artifactId = project.name.toLowerCase()
ext.publishing.groupId = 'eu.mihosoft.jcsg.ext.mesh'
ext.publishing.versionId = '0.4.1'
ext.publishing.versionId = '0.4.2'

ext.publishing.developerName = 'Michael Hoffer'
ext.publishing.developerAlias = 'miho'
Expand Down
159 changes: 113 additions & 46 deletions src/main/java/eu/mihosoft/jcsg/ext/mesh/MeshTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,15 @@ public static CSG optimize(
double maxEdgeLength,
int maxIter,
double creaseEdgeAngle) {
try {

Path tmpDir = Files.createTempDirectory("jcsgmeshopt");
Path stlFile = Paths.get(tmpDir.toAbsolutePath().toString(),
"csg.stl");

System.out.println("mesh-ext: csg file: " + stlFile);

Files.write(stlFile, csg.toStlString().getBytes());

String code = read("optimize-and-repair.lua");

String pathVariable = stlFile.toAbsolutePath().toString();//

if (System.getProperty("os.name").toLowerCase().contains("windows")) {
pathVariable = pathVariable.replace("\\", "\\\\");
}

code = code.replace("$filename$", "\""
+ pathVariable + "\"");
code = code.replace("$removeDoublesTOL$", "" + tol);
code = code.replace("$creaseEdgeAngle$", "" + creaseEdgeAngle);
code = code.replace("$resolveTOL$", "" + maxTol);
code = code.replace("$minEdgeLength$", "" + minEdgeLength);
code = code.replace("$maxEdgeLength$", "" + maxEdgeLength);
code = code.replace("$maxAdjIter$", "" + maxIter);

// code = code.replace("$edgeApprox$", "" + edgeApprox);
// code = code.replace("$edgeTriangleQuality$", "" + edgeTriangleQuality);
Shell.execute(tmpDir.toFile(), code).print().waitFor();

return STL.file(stlFile);

} catch (IOException e) {
e.printStackTrace(System.err);
throw new RuntimeException(
"optimization failed due to io exception", e);
}
return optimize(
"adjust-edge-length",
csg, tol, maxTol, minEdgeLength, maxEdgeLength,
-1, -1, maxIter, creaseEdgeAngle);
}

/**
* Optimizes and repairs the specified csg mesh object.
*
*
* <b>Note: </b>the size of the
* object during optimization can have a high impact on the overall
* optimization quality. Therefore, this method allows the specification of
Expand Down Expand Up @@ -164,8 +130,8 @@ public static CSG optimize(
}

/**
* Optimizes and repairs the specified csg mesh object.
*
* Optimizes and repairs the specified csg mesh object.
*
* <b>Note: </b>the size of the
* object during optimization can have a high impact on the overall
* optimization quality. Therefore, this method allows the specification of
Expand All @@ -184,18 +150,119 @@ public static CSG optimize(
* @return optimized csg mesh object
*/
public static CSG optimize(CSG csg,
double size,
double tol,
double size,
double tol,
double maxTol,
double minEdgeLength,
double maxEdgeLength,
int maxIter,
double creaseEdgeAngle) {
return scaleMinDimensionTo(csg, size,
(csgObj) -> optimize(csgObj, tol, maxTol,
minEdgeLength, maxEdgeLength));
}

/**
* Optimizes and repairs the specified csg mesh object.
*
* <b>Note: </b>the size of the
* object during optimization can have a high impact on the overall
* optimization quality. Therefore, this method allows the specification of
* the size at which the optimization is performed. After the optimization
* the object is returned at original size.
*
* @param csg csg to optimize
* @param size object size at which to perform the optimization (minimum
* dimension)
* @param tol default tolerance
* @param maxTol maximum tolerance
* @param minEdgeLength minimum edge length
* @param maxEdgeLength maximum edge length
* @param maxIter number of iterations for edge length adjustment
* @param creaseEdgeAngle angle threashold for crease edge marker
* @return optimized csg mesh object
*/
public static CSG optimize(CSG csg, double size, double tol,
double maxTol,
double minEdgeLength,
double maxEdgeLength,
double edgeApprox,
double edgeTriangleQuality,
int maxIter,
double creaseEdgeAngle) {
return scaleMinDimensionTo(csg, size,
(csgObj) -> optimize("adjust-edge-length-extended", csgObj, tol, maxTol,
minEdgeLength, maxEdgeLength,edgeApprox,edgeTriangleQuality,maxIter,creaseEdgeAngle));
}

/**
* Optimizes and repairs the specified csg mesh object.
*
* @param csg csg to optimize
* @param tol default tolerance
* @param maxTol maximum tolerance
* @param minEdgeLength minimum edge length
* @param maxEdgeLength maximum edge length
* @param maxIter number of iterations for edge length adjustment
* @param creaseEdgeAngle angle threashold for crease edge marker
* @return optimized csg mesh object
*/
private static CSG optimize(
String optType,
CSG csg, double tol,
double maxTol,
double minEdgeLength,
double maxEdgeLength,
double edgeApprox,
double edgeTriangleQuality,
int maxIter,
double creaseEdgeAngle) {
return scaleMinDimensionTo(csg, size,
(csgObj) -> optimize(csgObj, tol, maxTol,
minEdgeLength, maxEdgeLength));
try {

Path tmpDir = Files.createTempDirectory("jcsgmeshopt");
Path stlFile = Paths.get(tmpDir.toAbsolutePath().toString(),
"csg.stl");

System.out.println("mesh-ext: csg file: " + stlFile);

Files.write(stlFile, csg.toStlString().getBytes());

String code = read("optimize-and-repair.lua");

String pathVariable = stlFile.toAbsolutePath().toString();//

if (System.getProperty("os.name").toLowerCase().contains("windows")) {
pathVariable = pathVariable.replace("\\", "\\\\");
}

code = code.replace("$optType$", "\""
+ optType + "\"");
code = code.replace("$fileName$", "\""
+ pathVariable + "\"");
code = code.replace("$removeDoublesTOL$", "" + tol);
code = code.replace("$creaseEdgeAngle$", "" + creaseEdgeAngle);
code = code.replace("$resolveTOL$", "" + maxTol);
code = code.replace("$minEdgeLength$", "" + minEdgeLength);
code = code.replace("$maxEdgeLength$", "" + maxEdgeLength);
code = code.replace("$maxAdjIter$", "" + maxIter);

code = code.replace("$edgeApprox$", "" + edgeApprox);
code = code.replace("$edgeTriangleQuality$", "" + edgeTriangleQuality);

// code = code.replace("$edgeApprox$", "" + edgeApprox);
// code = code.replace("$edgeTriangleQuality$", "" + edgeTriangleQuality);
Shell.execute(tmpDir.toFile(), code).print().waitFor();

return STL.file(stlFile);

} catch (IOException e) {
e.printStackTrace(System.err);
throw new RuntimeException(
"optimization failed due to io exception", e);
}
}


/**
* Scales the minimum CSG dimension to the specified value, invokes the
* specified function and rescales the specified CSG object to its original
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--
-- optimize.lua
--
-- Copyright 2016 Michael Hoffer <[email protected]>. All rights reserved.
-- Copyright 2016-2017 Michael Hoffer <[email protected]>. All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification, are
-- permitted provided that the following conditions are met:
Expand Down Expand Up @@ -33,18 +33,20 @@
--

-- script parameters
fileName = $filename$
fileName = $fileName$
removeDoublesTOL = $removeDoublesTOL$
creaseEdgeAngle = $creaseEdgeAngle$
resolveTOL = $resolveTOL$
minEdgeLength = $minEdgeLength$
maxEdgeLength = $maxEdgeLength$
maxAdjIter = $maxAdjIter$

-- might be useful if we use new adjustEdgeLengthExtended
-- (currently, not stable enough, might be just a parameter problem)
--edgeApprox = $edgeApprox$
--edgeTriangleQuality = $edgeTriangleQuality$
-- selects optimization type
optType = $optType$

-- necessary if we use new adjustEdgeLengthExtended
edgeApprox = $edgeApprox$
edgeTriangleQuality = $edgeTriangleQuality$

-- check whether boundery edges exist
function hasBoundaryEdges(meshP)
Expand Down Expand Up @@ -102,8 +104,14 @@ SelectCreaseEdges(mesh,creaseEdgeAngle)
MarkSelection(mesh)

print("> retriangulate")
AdjustEdgeLength(mesh, minEdgeLength, maxEdgeLength, maxAdjIter, true, true)
--AdjustEdgeLengthExtended(mesh, minEdgeLength, maxEdgeLength, edgeApprox, edgeTriangleQuality, maxAdjIter, true)
if optType=="adjust-edge-length" then
AdjustEdgeLength(mesh, minEdgeLength, maxEdgeLength, maxAdjIter, true, true)
elseif optType=="adjust-edge-length-extended" then
AdjustEdgeLengthExtended(mesh, minEdgeLength, maxEdgeLength, edgeApprox, edgeTriangleQuality, maxAdjIter, true)
else
print(" -> ERROR: optimization type '"..optType.."' not supported.")
do return 1 end -- quit with exit code
end

ClearMarks(mesh)

Expand Down
89 changes: 89 additions & 0 deletions src/main/resources/eu/mihosoft/jcsg/ext/mesh/repair.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
--
-- optimize.lua
--
-- Copyright 2016-2017 Michael Hoffer <[email protected]>. All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification, are
-- permitted provided that the following conditions are met:
--
-- 1. Redistributions of source code must retain the above copyright notice, this list of
-- conditions and the following disclaimer.
--
-- 2. Redistributions in binary form must reproduce the above copyright notice, this list
-- of conditions and the following disclaimer in the documentation and/or other materials
-- provided with the distribution.
--
-- THIS SOFTWARE IS PROVIDED BY Michael Hoffer <[email protected]> "AS IS" AND ANY EXPRESS OR IMPLIED
-- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Hoffer <[email protected]> OR
-- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
-- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--
-- The views and conclusions contained in the software and documentation are those of the
-- authors and should not be interpreted as representing official policies, either expressed
-- or implied, of Michael Hoffer <[email protected]>.
--
-- Date: 25.12.16
-- Time: 19:30
-- Author: Michael Hoffer <[email protected]>
--

-- script parameters
fileName = $fileName$
removeDoublesTOL = $removeDoublesTOL$
resolveTOL = $resolveTOL$



-- check whether boundery edges exist
function hasBoundaryEdges(meshP)

SelectBoundaryEdges(meshP)
local vec_center = Vec3d()
hasSelection = GetSelectionCenter(meshP,vec_center)

return hasSelection
end

local mesh = Mesh()
print("> loading "..fileName)
if LoadMesh(mesh, fileName)==false then
print(" -> ERROR while loading file.")
end

SelectAll(mesh)

numRemoved = RemoveDoubles(mesh, removeDoublesTOL)

print("> removed "..numRemoved.." doubles with TOL "..removeDoublesTOL)

ClearSelection(mesh)

local counter = 0;
local maxIter = 10;
local tolInc = (resolveTOL-removeDoublesTOL)/maxIter

print("> fixing mesh (remove bnd-edges)")
while hasBoundaryEdges(mesh) and counter < maxIter do
local currentTol = removeDoublesTOL + tolInc*counter
print(" -> attempt #"..counter.." with TOL "..currentTol)
ResolveEdgeIntersection(mesh, currentTol)

SelectAll(mesh)

numRemoved = RemoveDoubles(mesh, currentTol)

print(" -> removed "..numRemoved.." doubles with TOL "..currentTol)

ClearSelection(mesh)
counter=counter+1
end

if counter >= maxIter then
print(" -> ERROR while fixing mesh")
do return 1 end -- quit with exit code
end

0 comments on commit 1da2c41

Please sign in to comment.