Rumba C++ SDK
BrushPlugin.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2019
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include "Export.h"
18 #include "Mesh.h"
19 #include "Scene.h"
20 #include "ToolPlugin.h"
21 
22 namespace maquina
23 {
24 
25 // Base class of the brush user data
27 {
28 public:
29  virtual ~BrushUserData() {}
30 };
31 
32 /* Base class to create brush plug-ins.
33  By deriving this class, create tools such as vertex color brushes or sculpt brushes.
34  The class has an API to compute and inspect the geometries which could be hit by the brush.
35 */
37 {
38 public:
39 
41  enum class BrushType
42  {
43  surface = 0,
44  geodesic,
45  volume,
46  fill
47  };
48 
49  BrushPlugin(Node* node);
50  BrushPlugin operator=(const BrushPlugin& ) = delete;
51 
53  void activate() override;
54  void deactivate() override;
55  void on_mouse_press( const CameraContext& camera, const InteractionContext& interaction ) override;
56  void on_mouse_move( const CameraContext& camera, const InteractionContext& interaction ) override;
57  void on_mouse_release( const CameraContext& camera, const InteractionContext& interaction ) override;
58  bool override_selection() override;
59  bool track_mouse() override;
60  bool manipulates() override;
61 
63 
65  virtual void stroke_begin(int modifiers);
66 
68 
72  virtual void stroke_update(
73  bool first,
74  bool finished
75  );
76 
78 
82  virtual void update_weights(
83  bool first,
84  const Imath::M44f& previous_brush_matrix
85  );
86 
88 
94  virtual bool do_update_weights() { return true; }
95 
97 
103  virtual float weight(float /* distance */) { return 1.f; }
104 
106 
114  virtual void update_brush_matrix(const CameraContext& camera, const InteractionContext& interaction, bool& visible, Imath::M44f& brush_matrix, bool on_press);
115 
117 
123  virtual float radius();
124 
126  void set_radius(float radius);
127 
129 
139  virtual float flow();
140 
142 
148  virtual bool multi_objects();
149 
151 
157  virtual float geodesic_dt();
158 
160 
166  virtual SceneConst scene();
167 
169 
175  virtual BrushType brush_type();
176 
177  /* The brush object creates and hold one GeometryData object per "paintable" geometry.
178  * It also hold the current vertex weight and the accumulation buffer of the brush on the geometry.
179  */
181  {
182  public:
183  GeometryData(const std::string& path,
184  const Mesh& displayed_mesh, const Imath::M44d& displayed_matrix,
185  const Mesh& input_mesh, const Imath::M44d& input_matrix,
186  bool side);
187 
189  bool dirty;
190 
192  const std::string path;
193 
196 
201 
204 
207 
212 
213  struct Vertex
214  {
215  Vertex(int index, float weight, float vertex_selection) : index(index), weight(weight), vertex_selection(vertex_selection) {}
216  int index;
217  float weight;
219  };
220 
222 
224  std::vector<Vertex> weights;
225 
227  std::vector<float> accum_weights;
228 
230  std::shared_ptr<BrushUserData> user_data;
231 
233  bool side;
234  };
235 
237 
238  gsl::span<GeometryData> geometry_data();
239 
241  Imath::V3f hit_pos_world() const;
242 
244  const Imath::V3f& hit_normal_world() const;
245 
247  Imath::V3f hit_pos_world_orig() const;
248 
250  const Imath::V3f& hit_normal_world_orig() const;
251 
253  const Imath::M44f& brush_matrix() const;
254 
256  const Imath::M44f& brush_matrix_orig() const;
257 
259  bool is_brush_visible() const;
260 
262  bool is_pressed() const;
263 
264 private:
265  std::vector<GeometryData> _geometry_data;
266 
267  // Once the mouse is pressed, initialize all the geometries which MAY by painted
268  void _init_geometries(const CameraContext& camera, const InteractionContext& interaction);
269 
270  void _update_displayed_geometry(const CameraContext& camera);
271 
272  bool _pressed;
273  Imath::M44f _brush_matrix;
274  Imath::M44f _brush_matrix_orig;
275  Imath::V3f _hit_normal;
276  Imath::V3f _hit_normal_orig;
277  bool _brush_visible;
278 
279  const GeometryData* _first_geometry = nullptr; // The first object hit by the stroke
280  bool _first_geometry_front_face = false; // Was the first hit front face ?
281 };
282 
283 inline gsl::span<BrushPlugin::GeometryData> BrushPlugin::geometry_data()
284 {
285  return _geometry_data;
286 }
287 
289 {
290  return _brush_matrix.translation();
291 }
292 
294 {
295  return _hit_normal;
296 }
297 
299 {
300  return _brush_matrix_orig.translation();
301 }
302 
304 {
305  return _hit_normal_orig;
306 }
307 
309 {
310  return _brush_matrix;
311 }
312 
314 {
315  return _brush_matrix_orig;
316 }
317 
318 inline bool BrushPlugin::is_brush_visible() const
319 {
320  return _brush_visible;
321 }
322 
323 inline bool BrushPlugin::is_pressed() const
324 {
325  return _pressed;
326 }
327 
328 }
const Imath::M44d input_inversed_matrix
The world to local mesh matrix.
Definition: BrushPlugin.h:210
const Imath::M44f & brush_matrix() const
Return the brush matrix which transforms from the brush local space to the world space.
Definition: BrushPlugin.h:308
const Mesh input_mesh
The input mesh of the deformer node.
Definition: BrushPlugin.h:206
A Tool node plug-in.
Definition: ToolPlugin.h:32
const Vec3< T > translation() const
Definition: ImathMatrix.h:2996
const Imath::M44d input_inversed_transposed_matrix
The mesh matrix to transform the normals from local to world.
Definition: BrushPlugin.h:211
Vertex(int index, float weight, float vertex_selection)
Definition: BrushPlugin.h:215
Definition: ImathFrame.h:42
Imath::V3f hit_pos_world() const
Return the brush position in world space.
Definition: BrushPlugin.h:288
Definition: ImathFrame.h:43
A mesh value.
Definition: Mesh.h:33
std::vector< Vertex > weights
The vertex weights computed since the last stroke update.
Definition: BrushPlugin.h:224
This class provides viewport space conversion services.
Definition: CameraContext.h:23
float vertex_selection
Definition: BrushPlugin.h:218
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
int index
Definition: BrushPlugin.h:216
virtual float weight(float)
Convert a distance in weight.
Definition: BrushPlugin.h:103
Definition: BrushPlugin.h:36
BufferV3f displayed_normals
The displayed mesh normals.
Definition: BrushPlugin.h:203
bool dirty
Set this flag to update its geometry.
Definition: BrushPlugin.h:189
const Imath::M44d displayed_inversed_matrix
The world to local mesh matrix.
Definition: BrushPlugin.h:199
Definition: BrushPlugin.h:26
#define MAQUINA_EXPORT
Definition: Export.h:31
An immutable scene node value.
Definition: Scene.h:35
Mesh displayed_mesh
The displayed mesh in the viewport, may not be the same than the output mesh of the deformer node if ...
Definition: BrushPlugin.h:195
const Imath::M44d displayed_inversed_transposed_matrix
The mesh matrix to transform the normals from local to world.
Definition: BrushPlugin.h:200
BrushType
The different brush types.
Definition: BrushPlugin.h:41
const Imath::V3f & hit_normal_world_orig() const
Return the brush normal in world space at the beginning of the stroke.
Definition: BrushPlugin.h:303
std::vector< float > accum_weights
The accumulated weights since the beginning of the stroke.
Definition: BrushPlugin.h:227
This interface is passed to the events and is implemented by the application. */. ...
Definition: InteractionContext.h:22
bool is_brush_visible() const
Returns true is the brush is visible.
Definition: BrushPlugin.h:318
const Imath::M44f & brush_matrix_orig() const
Return the brush matrix which transforms from the brush local space to the world space at the beginni...
Definition: BrushPlugin.h:313
bool is_pressed() const
Returns true is the brush is pressed by the user.
Definition: BrushPlugin.h:323
std::shared_ptr< BrushUserData > user_data
Optional user provided data. The data set in that field will be passed at the next stroke_update call...
Definition: BrushPlugin.h:230
Imath::V3f hit_pos_world_orig() const
Returns the world space brush position at the beginning of the stroke.
Definition: BrushPlugin.h:298
Definition: BrushPlugin.h:213
float weight
Definition: BrushPlugin.h:217
bool side
Where the brush is at the start of the stroke: true for left, false for right.
Definition: BrushPlugin.h:233
const Imath::M44d input_matrix
The input mesh precomputed matrices.
Definition: BrushPlugin.h:209
const std::string path
The mesh&#39;s path in the edited scene.
Definition: BrushPlugin.h:192
virtual bool do_update_weights()
Does the weights have to be updated at every stroke_update() ?
Definition: BrushPlugin.h:94
Definition: BrushPlugin.h:180
const Imath::V3f & hit_normal_world() const
Return the brush normal in world space.
Definition: BrushPlugin.h:293
virtual ~BrushUserData()
Definition: BrushPlugin.h:29
const Imath::M44d displayed_matrix
The displayed mesh precomputed matrices.
Definition: BrushPlugin.h:198
Base class of all Rumba nodes.
Definition: Node.h:37
gsl::span< GeometryData > geometry_data()
Return the brush&#39;s geometry data.
Definition: BrushPlugin.h:283