Debian Codegraph @

src/Base/Builder3D.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2011 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_BUILDER3D_H
27 #define BASE_BUILDER3D_H
28
29 // Std. configurations
30
31 #include <sstream>
32 #include <vector>
33 #include <cstdint>
34 #include <Base/Tools3D.h>
35 #ifndef FC_GLOBAL_H
36 # include <FCGlobal.h>
37 #endif
38
39 #include "Placement.h"
40
41 namespace Base
42 {
43 class Matrix4D;
44
45 class BaseExport ColorRGB
46 {
47 public:
48 ColorRGB();
49 ColorRGB(const ColorRGB&) = default;
50 ColorRGB(ColorRGB&&) = default;
51 explicit ColorRGB(float red, float green, float blue);
52 ~ColorRGB() = default;
53 ColorRGB& operator=(const ColorRGB&) = default;
54 ColorRGB& operator=(ColorRGB&&) = default;
55
56 float red() const
57 {
58 return Rgb.red;
59 }
60
61 float green() const
62 {
63 return Rgb.green;
64 }
65
66 float blue() const
67 {
68 return Rgb.blue;
69 }
70
71 private:
72 /*! Returns the clamped value in range [-1, +1] */
73 static float valueInRange(float value);
74 struct
75 {
76 float red;
77 float green;
78 float blue;
79 } Rgb;
80 };
81
82 class BaseExport DrawStyle
83 {
84 public:
85 enum class Style
86 {
87 Filled,
88 Lines,
89 Points,
90 Invisible
91 };
92
93 const char* styleAsString() const;
94 std::string patternAsString() const;
95 Style style = Style::Filled;
96 unsigned short pointSize = 2;
97 unsigned short lineWidth = 2;
98 unsigned short linePattern = 0xffff;
99 };
100
101 class BaseExport VertexOrdering
102 {
103 public:
104 enum class Ordering
105 {
106 UnknownOrdering,
107 Clockwise,
108 CounterClockwise
109 };
110
111 const char* toString() const;
112
113 Ordering ordering = Ordering::UnknownOrdering;
114 };
115
116 class BaseExport ShapeType
117 {
118 public:
119 enum class Type
120 {
121 UnknownShapeType,
122 Convex
123 };
124
125 const char* toString() const;
126
127 Type type = Type::UnknownShapeType;
128 };
129
130 class BaseExport BindingElement
131 {
132 public:
133 enum class Binding
134 {
135 Overall = 2,
136 PerPart = 3,
137 PerPartIndexed = 4,
138 PerFace = 5,
139 PerFaceIndexed = 6,
140 PerVertex = 7,
141 PerVertexIndexed = 8,
142 Default = Overall,
143 None = Overall
144 };
145
146 const char* bindingAsString() const;
147 Binding value = Binding::Overall;
148 };
149
150 class BaseExport PolygonOffset
151 {
152 public:
153 enum class Style
154 {
155 Filled,
156 Lines,
157 Points
158 };
159
160 const char* styleAsString() const;
161 float factor = 1.0F;
162 float units = 1.0F;
163 Style style = Style::Filled;
164 bool on = true;
165 };
166
167 class BaseExport Triangle
168 {
169 public:
170 explicit Triangle(const Base::Vector3f& pt1, const Base::Vector3f& pt2, const Base::Vector3f& pt3)
171 : pt1(pt1)
172 , pt2(pt2)
173 , pt3(pt3)
174 {}
175
176 const Base::Vector3f& getPoint1() const
177 {
178 return pt1;
179 }
180
181 const Base::Vector3f& getPoint2() const
182 {
183 return pt2;
184 }
185
186 const Base::Vector3f& getPoint3() const
187 {
188 return pt3;
189 }
190
191 private:
192 Base::Vector3f pt1;
193 Base::Vector3f pt2;
194 Base::Vector3f pt3;
195 };
196
197 class Indentation
198 {
199 int spaces = 0;
200
201 public:
202 void increaseIndent()
203 {
204 spaces += 2;
205 }
206 void decreaseIndent()
207 {
208 spaces -= 2;
209 }
210 int count() const
211 {
212 return spaces;
213 }
214 friend std::ostream& operator<<(std::ostream& os, Indentation ind)
215 {
216 for (int i = 0; i < ind.count(); i++) {
217 os << " ";
218 }
219 return os;
220 }
221 };
222
223 class BaseExport InventorOutput
224 {
225 public:
226 explicit InventorOutput(std::ostream& result, Indentation& indent);
227 std::ostream& stream();
228 std::ostream& write();
229 std::ostream& write(const char*);
230 std::ostream& write(const std::string&);
231 std::ostream& writeLine();
232 std::ostream& writeLine(const char*);
233 std::ostream& writeLine(const std::string&);
234 void increaseIndent();
235 void decreaseIndent();
236
237 private:
238 std::ostream& result;
239 Indentation& indent;
240 };
241
242 class BaseExport NodeItem
243 {
244 public:
245 virtual ~NodeItem() = default;
246 virtual void write(InventorOutput& out) const = 0;
247
248 protected:
249 NodeItem() = default;
250 NodeItem(const NodeItem&) = default;
251 NodeItem(NodeItem&&) = default;
252 NodeItem& operator=(const NodeItem&) = default;
253 NodeItem& operator=(NodeItem&&) = default;
254 };
255
256 /*!
257 * \brief The LabelItem class supports the SoLabel node.
258 */
259 class BaseExport LabelItem: public NodeItem
260 {
261 public:
262 explicit LabelItem(std::string text);
263 void write(InventorOutput& out) const override;
264
265 private:
266 std::string text;
267 };
268
269 /*!
270 * \brief The InfoItem class supports the SoInfo node.
271 */
272 class BaseExport InfoItem: public NodeItem
273 {
274 public:
275 explicit InfoItem(std::string text);
276 void write(InventorOutput& out) const override;
277
278 private:
279 std::string text;
280 };
281
282 /*!
283 * \brief The BaseColorItem class supports the SoBaseColor node.
284 */
285 class BaseExport BaseColorItem: public NodeItem
286 {
287 public:
288 explicit BaseColorItem(const ColorRGB& rgb);
289 void write(InventorOutput& out) const override;
290
291 private:
292 ColorRGB rgb;
293 };
294
295 class BaseExport PointItem: public NodeItem
296 {
297 public:
298 explicit PointItem(
299 const Base::Vector3f& point,
300 DrawStyle drawStyle,
301 const ColorRGB& rgb = ColorRGB {1.0F, 1.0F, 1.0F}
302 );
303 void write(InventorOutput& out) const override;
304
305 private:
306 Base::Vector3f point;
307 DrawStyle drawStyle;
308 ColorRGB rgb;
309 };
310
311 class BaseExport LineItem: public NodeItem
312 {
313 public:
314 explicit LineItem(
315 const Base::Line3f& line,
316 DrawStyle drawStyle,
317 const ColorRGB& rgb = ColorRGB {1.0F, 1.0F, 1.0F}
318 );
319 void write(InventorOutput& out) const override;
320
321 private:
322 Base::Line3f line;
323 DrawStyle drawStyle;
324 ColorRGB rgb;
325 };
326
327 class BaseExport MultiLineItem: public NodeItem
328 {
329 public:
330 /// add a line defined by a list of points whereat always a pair (i.e. a point and the following
331 /// point) builds a line.
332 explicit MultiLineItem(
333 std::vector<Vector3f> points,
334 DrawStyle drawStyle,
335 const ColorRGB& rgb = ColorRGB {1.0F, 1.0F, 1.0F}
336 );
337 void write(InventorOutput& out) const override;
338
339 private:
340 std::vector<Vector3f> points;
341 DrawStyle drawStyle;
342 ColorRGB rgb;
343 };
344
345 class BaseExport ArrowItem: public NodeItem
346 {
347 public:
348 explicit ArrowItem(
349 const Base::Line3f& line,
350 DrawStyle drawStyle,
351 const ColorRGB& rgb = ColorRGB {1.0F, 1.0F, 1.0F}
352 );
353 void write(InventorOutput& out) const override;
354
355 private:
356 Base::Line3f line;
357 DrawStyle drawStyle;
358 ColorRGB rgb;
359 };
360
361 class BaseExport BoundingBoxItem: public NodeItem
362 {
363 public:
364 explicit BoundingBoxItem(
365 const Vector3f& pt1,
366 const Vector3f& pt2,
367 DrawStyle drawStyle,
368 const ColorRGB& rgb = ColorRGB {1.0F, 1.0F, 1.0F}
369 );
370 void write(InventorOutput& out) const override;
371
372 private:
373 Vector3f pt1;
374 Vector3f pt2;
375 DrawStyle drawStyle;
376 ColorRGB rgb;
377 };
378
379 /*!
380 * \brief The MaterialItem class supports the SoMaterial node.
381 */
382 class BaseExport MaterialItem: public NodeItem
383 {
384 public:
385 void setAmbientColor(const std::vector<ColorRGB>& rgb);
386 void setDiffuseColor(const std::vector<ColorRGB>& rgb);
387 void setSpecularColor(const std::vector<ColorRGB>& rgb);
388 void setEmissiveColor(const std::vector<ColorRGB>& rgb);
389 void setShininess(const std::vector<float>& value);
390 void setTransparency(const std::vector<float>& value);
391 void write(InventorOutput& out) const override;
392
393 private:
394 void beginMaterial(InventorOutput& out) const;
395 void endMaterial(InventorOutput& out) const;
396 void writeAmbientColor(InventorOutput& out) const;
397 void writeDiffuseColor(InventorOutput& out) const;
398 void writeSpecularColor(InventorOutput& out) const;
399 void writeEmissiveColor(InventorOutput& out) const;
400 void writeShininess(InventorOutput& out) const;
401 void writeTransparency(InventorOutput& out) const;
402
403 private:
404 std::vector<ColorRGB> ambientColor;
405 std::vector<ColorRGB> diffuseColor;
406 std::vector<ColorRGB> specularColor;
407 std::vector<ColorRGB> emissiveColor;
408 std::vector<float> shininess;
409 std::vector<float> transparency;
410 };
411
412 /*!
413 * \brief The MaterialBindingItem class supports the SoMaterialBinding node.
414 */
415 class BaseExport MaterialBindingItem: public NodeItem
416 {
417 public:
418 MaterialBindingItem() = default;
419 explicit MaterialBindingItem(BindingElement::Binding);
420 void setValue(BindingElement::Binding bind);
421 void write(InventorOutput& out) const override;
422
423 private:
424 BindingElement value;
425 };
426
427 /*!
428 * \brief The DrawStyleItem class supports the SoDrawStyle node.
429 */
430 class BaseExport DrawStyleItem: public NodeItem
431 {
432 public:
433 DrawStyleItem() = default;
434 explicit DrawStyleItem(DrawStyle);
435 void setValue(DrawStyle value);
436 void write(InventorOutput& out) const override;
437
438 private:
439 DrawStyle style;
440 };
441
442 /*!
443 * \brief The ShapeHintsItem class supports the SoShapeHints node.
444 */
445 class BaseExport ShapeHintsItem: public NodeItem
446 {
447 public:
448 explicit ShapeHintsItem(float creaseAngle = 0.0F);
449 void setVertexOrdering(VertexOrdering::Ordering);
450 void setShapeType(ShapeType::Type);
451 void write(InventorOutput& out) const override;
452
453 private:
454 float creaseAngle = 0.0F;
455 VertexOrdering vertexOrdering;
456 ShapeType shapeType;
457 };
458
459 /*!
460 * \brief The PolygonOffsetItem class supports the SoPolygonOffset node.
461 */
462 class BaseExport PolygonOffsetItem: public NodeItem
463 {
464 public:
465 void setValue(PolygonOffset value);
466 void write(InventorOutput& out) const override;
467
468 private:
469 PolygonOffset offset;
470 };
471
472 /*!
473 * \brief The Coordinate3Item class supports the SoCoordinate3 node.
474 */
475 class BaseExport Coordinate3Item: public NodeItem
476 {
477 public:
478 explicit Coordinate3Item(std::vector<Vector3f> points);
479 void write(InventorOutput& out) const override;
480
481 private:
482 void beginPoint(InventorOutput& out) const;
483 void endPoint(InventorOutput& out) const;
484 std::vector<Vector3f> points;
485 };
486
487 /*!
488 * \brief The PointSetItem class supports the SoPointSet node.
489 */
490 class BaseExport PointSetItem: public NodeItem
491 {
492 public:
493 void write(InventorOutput& out) const override;
494 };
495
496 /*!
497 * \brief The LineSetItem class supports the SoLineSet node.
498 */
499 class BaseExport LineSetItem: public NodeItem
500 {
501 public:
502 void write(InventorOutput& out) const override;
503 };
504
505 /*!
506 * \brief The FaceSetItem class supports the SoFaceSet node.
507 */
508 class BaseExport FaceSetItem: public NodeItem
509 {
510 public:
511 explicit FaceSetItem(std::vector<int>);
512 void write(InventorOutput& out) const override;
513
514 private:
515 std::vector<int> indices;
516 };
517
518 /*!
519 * \brief The IndexedLineSetItem class supports the SoIndexedLineSet node.
520 */
521 class BaseExport IndexedLineSetItem: public NodeItem
522 {
523 public:
524 explicit IndexedLineSetItem(std::vector<int>);
525 void write(InventorOutput& out) const override;
526
527 private:
528 std::vector<int> indices;
529 };
530
531 /*!
532 * \brief The IndexedFaceSetItem class supports the SoIndexedFaceSet node.
533 */
534 class BaseExport IndexedFaceSetItem: public NodeItem
535 {
536 public:
537 explicit IndexedFaceSetItem(std::vector<int>);
538 void write(InventorOutput& out) const override;
539
540 private:
541 std::vector<int> indices;
542 };
543
544 /*!
545 * \brief The NormalItem class supports the SoNormal node.
546 */
547 class BaseExport NormalItem: public NodeItem
548 {
549 public:
550 explicit NormalItem(std::vector<Base::Vector3f> vec);
551 void write(InventorOutput& out) const override;
552
553 private:
554 void beginNormal(InventorOutput& out) const;
555 void endNormal(InventorOutput& out) const;
556 std::vector<Base::Vector3f> vector;
557 };
558
559 /*!
560 * \brief The MaterialBindingItem class supports the SoMaterialBinding node.
561 */
562 class BaseExport NormalBindingItem: public NodeItem
563 {
564 public:
565 NormalBindingItem() = default;
566 void setValue(BindingElement::Binding bind);
567 void write(InventorOutput& out) const override;
568
569 private:
570 BindingElement value;
571 };
572
573 /*!
574 * \brief The CylinderItem class supports the SoCylinder node.
575 */
576 class BaseExport CylinderItem: public NodeItem
577 {
578 public:
579 void setRadius(float);
580 void setHeight(float);
581 void write(InventorOutput& out) const override;
582
583 private:
584 float radius = 2.0F;
585 float height = 10.0F;
586 };
587
588 /*!
589 * \brief The ConeItem class supports the SoCone node.
590 */
591 class BaseExport ConeItem: public NodeItem
592 {
593 public:
594 void setBottomRadius(float);
595 void setHeight(float);
596 void write(InventorOutput& out) const override;
597
598 private:
599 float bottomRadius = 2.0F;
600 float height = 10.0F;
601 };
602
603 /*!
604 * \brief The SphereItem class supports the SoSphere node.
605 */
606 class BaseExport SphereItem: public NodeItem
607 {
608 public:
609 void setRadius(float);
610 void write(InventorOutput& out) const override;
611
612 private:
613 float radius = 2.0F;
614 };
615
616 /*!
617 * \brief The NurbsSurfaceItem class supports the SoNurbsSurface node.
618 */
619 class BaseExport NurbsSurfaceItem: public NodeItem
620 {
621 public:
622 void setControlPoints(int numU, int numV);
623 void setKnotVector(const std::vector<float>&, const std::vector<float>&);
624 void write(InventorOutput& out) const override;
625
626 private:
627 int numUControlPoints = 0;
628 int numVControlPoints = 0;
629 std::vector<float> uKnotVector;
630 std::vector<float> vKnotVector;
631 };
632
633 /*!
634 * \brief The Text2Item class supports the SoText2 node.
635 */
636 class BaseExport Text2Item: public NodeItem
637 {
638 public:
639 explicit Text2Item(std::string);
640 void write(InventorOutput& out) const override;
641
642 private:
643 std::string string;
644 };
645
646 /*!
647 * \brief The TransformItem class supports the SoTransform node.
648 */
649 class BaseExport TransformItem: public NodeItem
650 {
651 public:
652 explicit TransformItem(const Base::Placement&);
653 explicit TransformItem(const Base::Matrix4D&);
654 void write(InventorOutput& out) const override;
655
656 private:
657 Base::Placement placement;
658 };
659
660 /**
661 * This class does basically the same as Builder3D except that it writes the data
662 * directly into a given stream without buffering the output data in a string stream.
663 * Compared to file streams, string streams are quite slow when writing data with more
664 * than a few hundred lines. Due to performance reasons the user should use a file
665 * stream in this case.
666 * @author Werner Mayer
667 */
668 class BaseExport InventorBuilder
669 {
670 public:
671 /*!
672 * \brief Construction of an InventorBuilder instance.
673 * \param str - stream to write the content into
674 */
675 explicit InventorBuilder(std::ostream& str);
676 /*!
677 * \brief Adds the OpenInventor header.
678 */
679 void addHeader();
680 /*!
681 * \brief Destruction of an InventorBuilder instance
682 */
683 virtual ~InventorBuilder();
684 /*!
685 * \brief addNode
686 * Writes the content of the added node to the output stream.
687 */
688 void addNode(const NodeItem&);
689 /*!
690 * \brief Sets a separator node.
691 */
692 void beginSeparator();
693 /*!
694 * \brief Closes the last added separator node.
695 */
696 void endSeparator();
697
698 private:
699 void increaseIndent();
700 void decreaseIndent();
701
702 public:
703 InventorBuilder(const InventorBuilder&) = delete;
704 InventorBuilder(InventorBuilder&&) = delete;
705 void operator=(const InventorBuilder&) = delete;
706 void operator=(InventorBuilder&&) = delete;
707
708 private:
709 std::ostream& result;
710 Indentation indent;
711 };
712
713 /** A Builder class for 3D representations on App level
714 * On the application level nothing is known of the visual representation of data.
715 * Nevertheless it's often needed to see some 3D information, e.g. points, directions,
716 * when you program or debug an algorithm. Builder3D was made for this specific purpose.
717 * This class allows you to easily build up a 3D representation of some mathematical and
718 * algorithm internals. You can save this representation to a file and view it in an
719 * Inventor viewer, or send it to the log. In the case of using the log and a debug
720 * FreeCAD the representation will be loaded into the active viewer.
721 * \par
722 * The workflow goes as follows: Create the a Builder3D object and call the methods
723 * to insert the graphical elements. After that call either saveToLog() or saveToFile().
724 * \par
725 * Usage:
726 * \code
727 Base::Builder3D log3D;
728 for ( unsigned long i=0; i<pMesh->CountPoints(); i++ )
729 {
730 log3D.addSinglePoint(pMesh->GetPoint(i));
731 log3D.addText(pMesh->GetPoint(i),"Point");
732 ...
733 }
734 log3D.saveToLog();
735 * \endcode
736 * \see Base::ConsoleSingleton
737 */
738 class BaseExport Builder3D
739 {
740 public:
741 Builder3D();
742 ~Builder3D();
743
744 /// clear the string buffer
745 void clear();
746
747 /** @name write the result */
748 //@{
749 /// sends the result to the log and gui
750 void saveToLog();
751 /// save the result to a file (*.iv)
752 void saveToFile(const char* FileName);
753 //@}
754
755 /*!
756 * \brief addNode
757 * Writes the content of the added node to the output stream.
758 */
759 void addNode(const NodeItem&);
760 /*!
761 * \brief Sets a separator node.
762 */
763 void beginSeparator();
764 /*!
765 * \brief Closes the last added separator node.
766 */
767 void endSeparator();
768
769 Builder3D(const Builder3D&) = delete;
770 Builder3D(Builder3D&&) = delete;
771 Builder3D& operator=(const Builder3D&) = delete;
772 Builder3D& operator=(Builder3D&&) = delete;
773
774 private:
775 /// the result string
776 std::stringstream result;
777 InventorBuilder builder;
778 };
779
780 /**
781 * Loads an OpenInventor file.
782 * @author Werner Mayer
783 */
784 class BaseExport InventorLoader
785 {
786 public:
787 struct Face
788 {
789 Face(int32_t p1, int32_t p2, int32_t p3)
790 : p1(p1)
791 , p2(p2)
792 , p3(p3)
793 {}
794 int32_t p1, p2, p3;
795 };
796
797 explicit InventorLoader(std::istream& inp)
798 : inp(inp)
799 {}
800
801 /// Start the read process. Returns true if successful and false otherwise.
802 /// The obtained data can be accessed with the appropriate getter functions.
803 bool read();
804
805 /// Checks if the loaded data are valid
806 bool isValid() const;
807
808 /// Returns true if the data come from a non-indexed node as SoFaceSet.
809 /// This means that the read points contain duplicates.
810 bool isNonIndexed() const
811 {
812 return isnonindexed;
813 }
814
815 /// Return the vectors of an SoNormal node
816 const std::vector<Vector3f>& getVector() const
817 {
818 return vector;
819 }
820
821 /// Return the points of an SoCoordinate3 node
822 const std::vector<Vector3f>& getPoints() const
823 {
824 return points;
825 }
826
827 /// Return the faces of an SoIndexedFaceSet node
828 const std::vector<Face>& getFaces() const
829 {
830 return faces;
831 }
832
833 private:
834 void readNormals();
835 void readCoords();
836 void readIndexedFaceSet();
837 void readFaceSet();
838 template<typename T>
839 std::vector<T> readData(const char*) const;
840 std::vector<Vector3f> convert(const std::vector<float>&) const;
841 std::vector<Face> convert(const std::vector<int32_t>&) const;
842 std::vector<Face> convert(const std::vector<std::vector<int32_t>>&) const;
843 static std::vector<std::vector<int32_t>> split(const std::vector<int32_t>&);
844
845 private:
846 bool isnonindexed = false;
847 std::vector<Vector3f> vector;
848 std::vector<Vector3f> points;
849 std::vector<Face> faces;
850 std::istream& inp;
851 };
852
853 /*!
854 * Expects a string of the form "(x,y,z)" and creates a vector from it.
855 * If it fails then a std::exception is thrown.
856 * Supported type names are float or double
857 */
858 BaseExport Base::Vector3f stringToVector(std::string);
859
860 /*!
861 * Expects a string of the form "(x,y,z)" and creates a vector from it.
862 * If it fails then a std::exception is thrown.
863 * Supported type names are float or double
864 */
865 BaseExport std::string vectorToString(Vector3f);
866
867 } // namespace Base
868
869 #endif // BASE_BUILDER3D_H