Skip to content

Commit 7f9117b

Browse files
Evil-Spiritwhitequark
authored andcommittedSep 11, 2019
Calculate area of selected faces, if any.
1 parent 915f55a commit 7f9117b

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ New measurement/analysis features:
7272
* New option for displaying areas of closed contours.
7373
* When calculating volume of the mesh, volume of the solid from the current
7474
group is now shown alongside total volume of all solids.
75+
* When calculating area, and faces are selected, calculate area of those faces
76+
instead of the closed contour in the sketch.
7577
* When selecting a point and a line, projected distance to current
7678
workplane is displayed.
7779

‎src/mesh.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -1181,3 +1181,14 @@ double SMesh::CalculateVolume() const {
11811181
}
11821182
return vol;
11831183
}
1184+
1185+
double SMesh::CalculateSurfaceArea(const std::vector<uint32_t> &faces) const {
1186+
double area = 0.0;
1187+
for(uint32_t f : faces) {
1188+
for(const STriangle &t : l) {
1189+
if(f != t.meta.face) continue;
1190+
area += t.Area();
1191+
}
1192+
}
1193+
return area;
1194+
}

‎src/polygon.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ double STriangle::SignedVolume() const {
8787
return a.Dot(b.Cross(c)) / 6.0;
8888
}
8989

90+
double STriangle::Area() const {
91+
Vector ab = a.Minus(b);
92+
Vector cb = c.Minus(b);
93+
return ab.Cross(cb).Magnitude() / 2.0;
94+
}
95+
9096
bool STriangle::IsDegenerate() const {
9197
return a.OnLineSegment(b, c) ||
9298
b.OnLineSegment(a, c) ||

‎src/polygon.h

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class STriangle {
191191
bool Raytrace(const Vector &rayPoint, const Vector &rayDir,
192192
double *t, Vector *inters) const;
193193
double SignedVolume() const;
194+
double Area() const;
194195
bool IsDegenerate() const;
195196
};
196197

@@ -281,6 +282,7 @@ class SMesh {
281282
void PrecomputeTransparency();
282283
void RemoveDegenerateTriangles();
283284
double CalculateVolume() const;
285+
double CalculateSurfaceArea(const std::vector<uint32_t> &faces) const;
284286

285287
bool IsEmpty() const;
286288
void RemapFaces(Group *g, int remap);

‎src/solvespace.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,23 @@ void SolveSpaceUI::MenuAnalyze(Command id) {
797797

798798
case Command::AREA: {
799799
Group *g = SK.GetGroup(SS.GW.activeGroup);
800+
SS.GW.GroupSelection();
801+
auto const &gs = SS.GW.gs;
802+
double scale = SS.MmPerUnit();
803+
804+
if(gs.faces > 0) {
805+
std::vector<uint32_t> faces;
806+
faces.push_back(gs.face[0].v);
807+
if(gs.faces > 1) faces.push_back(gs.face[1].v);
808+
double area = g->displayMesh.CalculateSurfaceArea(faces);
809+
Message(_("The surface area of the selected faces is:\n\n"
810+
" %s\n\n"
811+
"Curves have been approximated as piecewise linear.\n"
812+
"This introduces error, typically of around 1%%."),
Has comments. Original line has comments.
813+
SS.MmToStringSI(area, /*dim=*/2).c_str());
814+
break;
815+
}
816+
800817
if(g->polyError.how != PolyError::GOOD) {
801818
Error(_("This group does not contain a correctly-formed "
802819
"2d closed area. It is open, not coplanar, or self-"

0 commit comments

Comments
 (0)
Please sign in to comment.