SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Face.hpp
1 //
2 // Face.hpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 #ifndef Face_hpp
29 #define Face_hpp
30 
31 #include <stdio.h>
32 #include <vector>
33 
34 #include "SUAPI-CppWrapper/model/DrawingElement.hpp"
35 #include "SUAPI-CppWrapper/model/TextureWriter.hpp"
36 #include "SUAPI-CppWrapper/model/UVHelper.hpp"
37 
38 #include <SketchUpAPI/model/face.h>
39 #include <SketchUpAPI/model/material.h>
40 #include <SketchUpAPI/model/loop.h>
41 
42 namespace CW {
43 
44 enum FacePointClass {
45  PointUnknown, // (indicates an error),
46  PointInside, // (point is on the face, not in a hole),
47  PointOnVertex, // (point touches a vertex),
48  PointOnEdge, // (point is on an edge),
49  PointOutside, // (point outside the face or in a hole),
50  PointNotOnPlane // (point off the face's plane).
51 };
52 
53 class Point3D;
54 class Plane3D;
55 class Vector3D;
56 class Material;
57 class Vertex;
58 class Loop;
59 class LoopInput;
60 class Edge;
61 
62 class Face :public DrawingElement {
63  private:
64  /**
65  * Creates a SUFaceRef object from an array of points that represent the outer loop.
66  * @param outer_loop vector of points for the vertices in the outer loop.
67  * @return SUFaceRef object with the defined loops. If there was an error, a SU_INVALID SUFaceRef will be returned.
68  */
69  static SUFaceRef create_face(std::vector<Point3D>& outer_points);
70  static SUFaceRef create_face(std::vector<Point3D>& outer_points, LoopInput& loop_input);
71 
72  /**
73  * Creates a SUFaceRef object from an array of points that represent the loops.
74  * @param outer_loop vector of points for the vertices in the outer loop.
75  * @param inner_loops vector of vectors of points. The first dimension represents the inner loops, and the second represents the points in each loop.
76  * @return SUFaceRef object with the defined loops. If there was an error, a SU_INVALID SUFaceRef will be returned.
77  */
78  //static SUFaceRef create_face(std::vector<Point3D> outer_loop, std::vector<std::vector<Point3D>> inner_loops);
79 
80  /**
81  * Creates a SUFaceRef derived from an existing Face object.
82  * @param face - Face object to derive the new SUFaceRef object from
83  * @return if the Face object is already attached to a model, its SUFaceRef object will be returned. If the Face object has not been attached to a model, a new SUFaceRef object will be created. Bear in mind all properties will not be copied in the latter case
84  */
85  static SUFaceRef copy_reference(const Face& face);
86 
87  //static SUFaceRef create_face(std::vector<Point3D> outer_loop, std::vector<std::vector<Point3D>> inner_loops, SUResult &create_result);
88  //static SUFaceRef check_face(SUFaceRef face, SUResult &create_result);
89 
90  public:
91  /**
92  * Constructor for creating a null object.
93  */
94  Face();
95 
96  /*
97  * Face constructor, which takes an array of points representing the outer loop, and an array of arrays of points representing the inner loops of the face. A new SUFaceRef object will be created within this class and handled internally.
98  */
99  Face(std::vector<Point3D>& outer_loop);
100  Face(std::vector<Point3D>& outer_loop, LoopInput& loop_input);
101  //Face(std::vector<Point3D> outer_loop, std::vector<std::vector<Point3D>> inner_loops = {{}});
102 
103  /*
104  * Face constructor that essentially wraps around an already created SUFaceRef object.
105  * @param SUFaceRef* pointer to the face.
106  * @param bool false if the face should be released when this class object is destroyed. True, if the destruction of the face object is handled elsewhere (use with caution).
107  */
108  Face(SUFaceRef face, bool attached = true);
109 
110  /** Copy constructor */
111  Face(const Face& other);
112 
113  /** Destructor */
114  ~Face();
115 
116  /** Copy assignment operator */
117  Face& operator=(const Face& other);
118 
119  /*
120  * Returns the C-style face_ref object
121  */
122  SUFaceRef ref() const;
123 
124  /*
125  * The class object can be converted to a SUFaceRef without loss of data.
126  */
127  operator SUFaceRef() const;
128 
129  /*
130  * Returns whether the class is a valid SU object.
131  */
132  operator bool() const;
133 
134  /**
135  * NOT operator. Checks if the object is valid.
136  * @return true if the curve is invalid
137  */
138  bool operator!() const;
139 
140  /*
141  * Retrieves the area of a face in SU units.
142  * @return double area in square inches (SU units).
143  */
144  double area() const;
145 
146  /**
147  * Adds an inner loop to the face.
148  * @param points - a vector of points representing the vertices of the inner loop.
149  * @param loop_input - the loop input object with details about the edges
150  */
151  void add_inner_loop(std::vector<Point3D>& points, LoopInput &loop_input);
152 
153  /*
154  * Retrieves the material assigned to the back side of the face.
155  * @return Material object of the back side of the face
156  */
157  Material back_material() const;
158 
159  /*
160  * Sets the material assigned to the back side of the face.
161  * @param Material object or the name of a valid material.
162  * @return Material object of the back side of the face
163  */
164  Material back_material(const Material& material);
165 
166  /*
167  * determine if a given Point3d is on the referenced Face. The return value is calculated from this list:
168  PointUnknown (indicates an error),
169  PointInside (point is on the face, not in a hole),
170  PointOnVertex (point touches a vertex),
171  PointOnEdge (point is on an edge),
172  PointOutside (point outside the face or in a hole),
173  PointNotOnPlane (point off the face's plane).
174  * @param SUPoint3D object.
175  * @return FacePointClass enum indicating the status of the point relative to the face.
176  */
177  FacePointClass classify_point(const Point3D& point);
178 
179  /*
180  * Get an array of edges that bound the face, including the edges of inner loops.
181  * @return std::vector of Edge objects that bound the Face.
182  */
183  std::vector<Edge> edges();
184 
185  /*
186  * Retrieves a UVHelper object for use in texture manipulation on a face.
187  * @param front true if you want the texture coordinates for the front face, false if not. Defaults to true.
188  * @param back True if you want the texture coordinates for the back face, false if not. Defaults to true.
189  * @param TextureWriter object.
190  * @return a UVHelper object.
191  */
192  // TODO
193  //UVHelper get_UVHelper(bool front = true, bool back = true, TextureWriter tex_writer = TextureWriter());
194 
195  /*
196  * Returns a vector representing the projection for either the front or back side of the face.
197  * @param bool true for frontside, false for back side.
198  */
199  Vector3D get_texture_projection(const bool frontside) const;
200 
201  /*
202  * Gets an array of all of the inner loops that bound the face.
203  */
204  std::vector<Loop> inner_loops() const;
205 
206  /*
207  * Gets an array of all of the loops that bound the face. The first Loop is the outer loop.
208  */
209  std::vector<Loop> loops() const;
210 
211  /*
212  // TODO
213  PolygonMesh mesh();
214  */
215 
216  /*
217  * Retrieve the 3D vector normal to the face in the front direction.
218  */
219  Vector3D normal() const;
220 
221  /*
222  * Retrieves the outer loop that bounds the face.
223  */
224  Loop outer_loop() const;
225 
226  /*
227  * Retreives the plane of this face.
228  */
229  Plane3D plane() const;
230 
231  /*
232  * Positions a material on a face.
233  * The pt_array must contain 2, 4, 6 or 8 points. The points are used in pairs to tell where a point in the texture image is positioned on the Face. The first point in each pair is a 3D point in the model. It should be a point on the Face. The second point in each pair of points is a 2D point that gives the (u,v) coordinates of a point in the image to match up with the 3D point.
234  * @param Material object to position.
235  * @param vector of Point3d objects used to position the material.
236  * @param bool true to position the texture on the front of the Face or false to position it on the back of the Face.
237  */
238  /** NOT POSSIBLE WITH C API - @see class MaterialInput **/
239  // bool position_material(const Material& material, const std::vector<Point3D>& pt_array, bool o_front);
240 
241  /*
242  * Reverses the face's orientation, meaning the front becomes the back.
243  * @return the reversed Face object
244  */
245  Face& reverse();
246 
247  /*
248  * Sets the texture projection direction.
249  * @param SUVector3D object representing the direction of the projection. Or bool true to remove texture projection.
250  * @param bool true for front side, false for back side.
251  * @return true on success
252  */
253  /** NOT POSSIBLE WITH C API - @see class MaterialInput **/
254  //bool set_texture_projection(const Vector3D& vector, bool frontside);
255  //bool set_texture_projection(bool remove, bool frontside);
256 
257  /*
258  * Gets an array of all of the vertices that bound the face.
259  * @return std::vector array of Vertex objects.
260  */
261  std::vector<Vertex> vertices() const;
262 
263 };
264 
265 } /* namespace CW */
266 #endif /* Face_hpp */
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
void add_inner_loop(std::vector< Point3D > &points, LoopInput &loop_input)
Definition: Face.cpp:187
std::vector< Loop > inner_loops() const
Definition: Face.cpp:315
Face & reverse()
Definition: Face.cpp:384
Face()
Definition: Face.cpp:93
~Face()
Definition: Face.cpp:134
Face & operator=(const Face &other)
Definition: Face.cpp:146
Definition: Color.hpp:34
std::vector< Vertex > vertices() const
Definition: Face.cpp:408
bool operator!() const
Definition: Face.cpp:168