Cumulia Illustrator Rendering Engine v1.0.0
A Rendering engine for industrial CAD/CAE model and optimized for greatest performance
 
Loading...
Searching...
No Matches
selector.h
1//##################################################################################################
2//
3// Copyright (c) 2024 Beijing Qiongqi Tech Co.,Ltd. All rights reserved.
4
5// This source code is confidential and proprietary to Beijing Qiongqi Tech Co.,Ltd(The Holder).
6// Any unauthorized use, copying, modification, or distribution of the code is strictly prohibited.
7// Any user shall obtain authorizaition from the Holder before modifying the source code. And the user shall not
8// sublicense, sell, distribute, or transfer the source code, whether in original or modified form, to any third party
9// without the prior written consent of the Holder.
10
11// This copyright notice and permission grant shall be included in all copies or substantial portions of the source code.
12
13// Author Cumulia Illustrator
14// Date 2024-05-01
15// Version V1.0.0
16//##################################################################################################
17
18#pragma once
19
20#include "builder.h"
21#include "producer.h"
22#include "consumer.h"
23
24#include <cilcore.h>
25
26#include <memory>
27#include <vector>
28#include <set>
29#include <functional>
30#include <algorithm>
31
32namespace cil
33{
35 {
36 None,
37 View,
38 Entity,
39 All
40 };
41
43 {
44 public:
45 std::shared_ptr<Renderer> renderer;
46 std::shared_ptr<Camera> camera;
47 std::shared_ptr<Vector2i> backgroundSize;
48 std::shared_ptr<Vector2i> windowSize;
49 };
50
51 template <typename T>
53 {
54 public:
55 std::shared_ptr<std::vector<std::shared_ptr<T>>> entities;
56 std::function<std::shared_ptr<Node>(std::shared_ptr<T>)> entity2node;
57 };
58
59 template <typename T>
61 {
62 public:
64 {
65 }
66
67 virtual ~Selector()
68 {
69 }
70
71 virtual void update(SelectorUpdateMode mode) = 0;
72 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities) = 0;
73 };
74
75 template <typename T>
76 class EntitySelector : public Selector<T>
77 {
78 protected:
79 std::shared_ptr<EntityBuilder<T>> m_builder;
80 std::shared_ptr<EntityProducer> m_producer;
82
83 private:
84 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
85 std::shared_ptr<Image2D> m_bufferImage;
86
87 public:
89 {
90 }
91
93 {
94 }
95
96 virtual void update(SelectorUpdateMode mode)
97 {
98 switch (mode)
99 {
101 this->m_bufferImage = nullptr;
102 break;
105 {
106 this->m_builtNodes = nullptr;
107 this->m_bufferImage = nullptr;
108 break;
109 }
110 default: break;
111 }
112 }
113
114 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
115 {
116 this->m_builder->setEntities(entities);
118 }
119
120 std::shared_ptr<T> select(int x, int y)
121 {
122 this->buildNodes();
123 if (this->m_buffered)
124 {
125 this->produceBufferImage();
126 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
127 auto pixelIndex = consumer->select(x, y);
128 unsigned int entityIndex = ((unsigned int*)this->m_bufferImage->data())[pixelIndex];
129 return entityIndex != 0XFFFFFFFF ? (*this->m_builder->entities())[entityIndex] : nullptr;
130 }
131 else
132 {
133 this->m_producer->toPixel(x, y);
134 auto image = this->m_producer->produce(this->m_builtNodes);
135 unsigned int entityIndex = ((unsigned int*)image->data())[0];
136 return entityIndex != 0XFFFFFFFF ? (*this->m_builder->entities())[entityIndex] : nullptr;
137 }
138 }
139
140 std::vector<std::shared_ptr<T>> multiSelect(int x1, int y1, int x2, int y2)
141 {
142 this->buildNodes();
143 if (this->m_buffered)
144 {
145 this->produceBufferImage();
146 auto consumer = std::make_shared<BoxConsumer>(this->m_producer->windowSize());
147 std::vector<int> pixelIndices = consumer->select(x1, y1, x2, y2);
148
149 std::set<unsigned int> entityIndices;
150 auto imageData = (unsigned int*)this->m_bufferImage->data();
151 for (auto pixelIndex : pixelIndices)
152 {
153 entityIndices.insert(imageData[pixelIndex]);
154 }
155 entityIndices.erase(0XFFFFFFFF);
156
157 std::vector<std::shared_ptr<T>> result;
158 auto& entities = *this->m_builder->entities();
159 for (auto entityIndex : entityIndices)
160 {
161 result.push_back(entities[entityIndex]);
162 }
163
164 return result;
165 }
166 else
167 {
168 this->m_producer->toBox(x1, y1, x2, y2);
169 auto image = this->m_producer->produce(this->m_builtNodes);
170
171 std::set<unsigned int> entityIndices;
172 auto imageData = (unsigned int*)image->data();
173 auto length = image->width() * image->height();
174 for (int i = 0; i < length; ++i)
175 {
176 entityIndices.insert(imageData[i]);
177 }
178 entityIndices.erase(0XFFFFFFFF);
179
180 std::vector<std::shared_ptr<T>> result;
181 auto& entities = *this->m_builder->entities();
182 for (auto entityIndex : entityIndices)
183 {
184 result.push_back(entities[entityIndex]);
185 }
186
187 return result;
188 }
189 }
190
191 private:
192 void buildNodes()
193 {
194 if (!this->m_builtNodes)
195 {
196 this->m_builtNodes = this->m_builder->build();
197 this->m_producer->updatePass();
198 }
199 }
200
201 void produceBufferImage()
202 {
203 if (!this->m_bufferImage) {
204 this->m_producer->toWindow();
205 this->m_bufferImage = this->m_producer->produce(this->m_builtNodes);
206 }
207 }
208 };
209
210 template <typename T>
211 class FaceSelector : public EntitySelector<T>
212 {
213 public:
214 FaceSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
215 : EntitySelector<T>()
216 {
217 this->m_builder = std::make_shared<FaceBuilder<T>>(options->entities, options->entity2node);
218 this->m_producer = std::make_shared<FaceProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
219 this->m_buffered = buffered;
220 }
221
223 {
224 }
225 };
226
227 template <typename T>
229 {
230 public:
231 VertexSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
232 : EntitySelector<T>()
233 {
234 this->m_builder = std::make_shared<VertexBuilder<T>>(options->entities, options->entity2node);
235 this->m_producer = std::make_shared<VertexProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
236 this->m_buffered = buffered;
237 }
238
240 {
241 }
242 };
243
244 template <typename T>
245 class EdgeSelector : public EntitySelector<T>
246 {
247 public:
248 EdgeSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
249 : EntitySelector<T>()
250 {
251 this->m_builder = std::make_shared<EdgeBuilder<T>>(options->entities, options->entity2node);
252 this->m_producer = std::make_shared<EdgeProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
253 this->m_buffered = buffered;
254 }
255
257 {
258 }
259 };
260
261 template <typename T>
262 class AttributeSelector : public Selector<T>
263 {
264 protected:
265 std::shared_ptr<AttributeBuilder<T>> m_builder;
266 std::shared_ptr<AttributeProducer> m_producer;
268
269 private:
270 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
271 std::vector<std::shared_ptr<Image2D>> m_bufferImages;
272
273 public:
275 : Selector<T>()
276 {
277 }
278
280 {
281 }
282
283 virtual void update(SelectorUpdateMode mode)
284 {
285 switch (mode)
286 {
288 this->m_bufferImages.clear();
289 break;
292 {
293 this->m_builtNodes = nullptr;
294 this->m_bufferImages.clear();
295 break;
296 }
297 default: break;
298 }
299 }
300
301 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
302 {
303 this->m_builder->setEntities(entities);
305 }
306
307 std::shared_ptr<Vector3f> select(int x, int y)
308 {
309 this->buildNodes();
310 if (this->m_buffered)
311 {
312 this->produceBufferImages();
313 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
314 auto pixelIndex = consumer->select(x, y);
315 unsigned int value = ((unsigned int*)this->m_bufferImages[0]->data())[pixelIndex];
316 if (value != 0XFFFFFFFF)
317 {
318 auto x = ((float*)this->m_bufferImages[0]->data())[pixelIndex];
319 auto y = ((float*)this->m_bufferImages[1]->data())[pixelIndex];
320 auto z = ((float*)this->m_bufferImages[2]->data())[pixelIndex];
321
322 return _vec3(x, y, z);
323 }
324 else
325 {
326 return nullptr;
327 }
328 }
329 else
330 {
331 this->m_producer->toPixel(x, y);
332 auto images = this->m_producer->produce(this->m_builtNodes);
333 unsigned int value = ((unsigned int*)images[0]->data())[0];
334 if (value != 0XFFFFFFFF)
335 {
336 auto x = ((float*)images[0]->data())[0];
337 auto y = ((float*)images[1]->data())[0];
338 auto z = ((float*)images[2]->data())[0];
339
340 return _vec3(x, y, z);
341 }
342 else
343 {
344 return nullptr;
345 }
346 }
347 }
348
349 private:
350 void buildNodes()
351 {
352 if (!this->m_builtNodes)
353 {
354 this->m_builtNodes = this->m_builder->build();
355 this->m_producer->updatePass();
356 }
357 }
358
359 void produceBufferImages()
360 {
361 if (this->m_bufferImages.size() == 0) {
362 this->m_producer->toWindow();
363 this->m_bufferImages = this->m_producer->produce(this->m_builtNodes);
364 }
365 }
366 };
367
368 template <typename T>
370 {
371 public:
372 PositionSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
373 : AttributeSelector<T>()
374 {
375 this->m_builder = std::make_shared<PositionBuilder<T>>(options->entities, options->entity2node);
376 this->m_producer = std::make_shared<PositionProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
377 this->m_buffered = buffered;
378 }
379
381 {
382 }
383 };
384
385 template <typename T>
387 {
388 public:
389 NormalSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
390 : AttributeSelector<T>()
391 {
392 this->m_builder = std::make_shared<NormalBuilder<T>>(options->entities, options->entity2node);
393 this->m_producer = std::make_shared<NormalProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
394 this->m_buffered = buffered;
395 }
396
398 {
399 }
400 };
401
402 template <typename T>
403 class PrimitiveSelector : public Selector<T>
404 {
405 protected:
406 std::shared_ptr<PrimitiveBuilder<T>> m_builder;
407 std::shared_ptr<PrimitiveProducer> m_producer;
409
410 private:
411 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
412 std::shared_ptr<Image2D> m_bufferImage;
413
414 public:
415 PrimitiveSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
416 : Selector<T>()
417 {
418 this->m_builder = std::make_shared<PrimitiveBuilder<T>>(options->entities, options->entity2node);
419 this->m_producer = std::make_shared<PrimitiveProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
420 this->m_buffered = buffered;
421 }
422
424 {
425 }
426
427 virtual void update(SelectorUpdateMode mode)
428 {
429 switch (mode)
430 {
432 this->m_bufferImage = nullptr;
433 break;
436 {
437 this->m_builtNodes = nullptr;
438 this->m_bufferImage = nullptr;
439 break;
440 }
441 default: break;
442 }
443 }
444
445 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
446 {
447 this->m_builder->setEntities(entities);
449 }
450
451 unsigned int select(int x, int y)
452 {
453 this->buildNodes();
454 if (this->m_buffered)
455 {
456 this->produceBufferImage();
457 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
458 auto pixelIndex = consumer->select(x, y);
459 unsigned int primitiveIndex = ((unsigned int*)this->m_bufferImage->data())[pixelIndex];
460
461 return primitiveIndex;
462 }
463 else
464 {
465 this->m_producer->toPixel(x, y);
466 auto image = this->m_producer->produce(this->m_builtNodes);
467 unsigned int primitiveIndex = ((unsigned int*)image->data())[0];
468
469 return primitiveIndex;
470 }
471 }
472
473 std::vector<unsigned int> multiSelect(int x1, int y1, int x2, int y2)
474 {
475 this->buildNodes();
476 if (this->m_buffered)
477 {
478 this->produceBufferImage();
479 auto consumer = std::make_shared<BoxConsumer>(this->m_producer->windowSize());
480 std::vector<int> pixelIndices = consumer->select(x1, y1, x2, y2);
481
482 std::set<unsigned int> primitiveIndices;
483 auto imageData = (unsigned int*)this->m_bufferImage->data();
484 for (auto pixelIndex : pixelIndices)
485 {
486 primitiveIndices.insert(imageData[pixelIndex]);
487 }
488 primitiveIndices.erase(0XFFFFFFFF);
489
490 return std::vector<unsigned int>(primitiveIndices.begin(), primitiveIndices.end());
491 }
492 else
493 {
494 this->m_producer->toBox(x1, y1, x2, y2);
495 auto image = this->m_producer->produce(this->m_builtNodes);
496 auto imageData = (unsigned int*)image->data();
497
498 std::set<unsigned int> primitiveIndices;
499 auto length = image->width() * image->height();
500 for (int i = 0; i < length; ++i)
501 {
502 primitiveIndices.insert(imageData[i]);
503 }
504 primitiveIndices.erase(0XFFFFFFFF);
505
506 return std::vector<unsigned int>(primitiveIndices.begin(), primitiveIndices.end());
507 }
508 }
509
510 private:
511 void buildNodes()
512 {
513 if (!this->m_builtNodes)
514 {
515 this->m_builtNodes = this->m_builder->build();
516 this->m_producer->updatePass();
517 }
518 }
519
520 void produceBufferImage()
521 {
522 if (!this->m_bufferImage) {
523 this->m_producer->toWindow();
524 this->m_bufferImage = this->m_producer->produce(this->m_builtNodes);
525 }
526 }
527 };
528
529 std::shared_ptr<SelectorEngine> _selectorEngine(const std::shared_ptr<Renderer>& renderer, const std::shared_ptr<Camera>& camera, const std::shared_ptr<Vector2i>& backgroundSize, const std::shared_ptr<Vector2i>& windowSize);
530
531 template <typename T>
532 std::shared_ptr<SelectorOptions<T>> _selectorOptions(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities, const std::function<std::shared_ptr<Node>(std::shared_ptr<T>)>& entity2node)
533 {
534 auto options = std::make_shared<SelectorOptions<T>>();
535 options->entities = entities;
536 options->entity2node = entity2node;
537
538 return options;
539 }
540
541 template <typename T>
542 std::shared_ptr<FaceSelector<T>> _faceSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
543 {
544 return std::make_shared<FaceSelector<T>>(engine, options, buffered);
545 }
546
547 template <typename T>
548 std::shared_ptr<VertexSelector<T>> _vertexSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
549 {
550 return std::make_shared<VertexSelector<T>>(engine, options, buffered);
551 }
552
553 template <typename T>
554 std::shared_ptr<EdgeSelector<T>> _edgeSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
555 {
556 return std::make_shared<EdgeSelector<T>>(engine, options, buffered);
557 }
558
559 template <typename T>
560 std::shared_ptr<PositionSelector<T>> _positionSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
561 {
562 return std::make_shared<PositionSelector<T>>(engine, options, buffered);
563 }
564
565 template <typename T>
566 std::shared_ptr<NormalSelector<T>> _normalSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
567 {
568 return std::make_shared<NormalSelector<T>>(engine, options, buffered);
569 }
570
571 template <typename T>
572 std::shared_ptr<PrimitiveSelector<T>> _primitiveSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
573 {
574 return std::make_shared<PrimitiveSelector<T>>(engine, options, buffered);
575 }
576}
Definition selector.h:263
virtual void update(SelectorUpdateMode mode)
Definition selector.h:283
AttributeSelector()
Definition selector.h:274
std::shared_ptr< AttributeBuilder< T > > m_builder
Definition selector.h:265
std::shared_ptr< AttributeProducer > m_producer
Definition selector.h:266
virtual ~AttributeSelector()
Definition selector.h:279
bool m_buffered
Definition selector.h:267
std::shared_ptr< Vector3f > select(int x, int y)
Definition selector.h:307
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:301
Definition selector.h:246
virtual ~EdgeSelector()
Definition selector.h:256
EdgeSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:248
Definition selector.h:77
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:114
std::shared_ptr< EntityBuilder< T > > m_builder
Definition selector.h:79
EntitySelector()
Definition selector.h:88
std::vector< std::shared_ptr< T > > multiSelect(int x1, int y1, int x2, int y2)
Definition selector.h:140
std::shared_ptr< EntityProducer > m_producer
Definition selector.h:80
virtual ~EntitySelector()
Definition selector.h:92
bool m_buffered
Definition selector.h:81
std::shared_ptr< T > select(int x, int y)
Definition selector.h:120
virtual void update(SelectorUpdateMode mode)
Definition selector.h:96
Definition selector.h:212
FaceSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:214
virtual ~FaceSelector()
Definition selector.h:222
Definition selector.h:387
NormalSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:389
virtual ~NormalSelector()
Definition selector.h:397
Definition selector.h:370
virtual ~PositionSelector()
Definition selector.h:380
PositionSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:372
Definition selector.h:404
unsigned int select(int x, int y)
Definition selector.h:451
bool m_buffered
Definition selector.h:408
std::vector< unsigned int > multiSelect(int x1, int y1, int x2, int y2)
Definition selector.h:473
virtual void update(SelectorUpdateMode mode)
Definition selector.h:427
PrimitiveSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:415
std::shared_ptr< PrimitiveBuilder< T > > m_builder
Definition selector.h:406
std::shared_ptr< PrimitiveProducer > m_producer
Definition selector.h:407
virtual ~PrimitiveSelector()
Definition selector.h:423
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:445
Definition selector.h:43
std::shared_ptr< Camera > camera
Definition selector.h:46
std::shared_ptr< Vector2i > backgroundSize
Definition selector.h:47
std::shared_ptr< Vector2i > windowSize
Definition selector.h:48
std::shared_ptr< Renderer > renderer
Definition selector.h:45
Definition selector.h:53
std::function< std::shared_ptr< Node >(std::shared_ptr< T >)> entity2node
Definition selector.h:56
std::shared_ptr< std::vector< std::shared_ptr< T > > > entities
Definition selector.h:55
Definition selector.h:61
virtual ~Selector()
Definition selector.h:67
Selector()
Definition selector.h:63
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)=0
virtual void update(SelectorUpdateMode mode)=0
Definition selector.h:229
VertexSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:231
virtual ~VertexSelector()
Definition selector.h:239
Definition decal.h:23
SelectorUpdateMode
Definition selector.h:35
std::shared_ptr< Vector3f > _vec3()
std::shared_ptr< VertexSelector< T > > _vertexSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:548
std::shared_ptr< NormalSelector< T > > _normalSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:566
std::shared_ptr< FaceSelector< T > > _faceSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:542
std::shared_ptr< PrimitiveSelector< T > > _primitiveSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:572
std::shared_ptr< SelectorEngine > _selectorEngine(const std::shared_ptr< Renderer > &renderer, const std::shared_ptr< Camera > &camera, const std::shared_ptr< Vector2i > &backgroundSize, const std::shared_ptr< Vector2i > &windowSize)
std::shared_ptr< EdgeSelector< T > > _edgeSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:554
std::shared_ptr< PositionSelector< T > > _positionSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:560
std::shared_ptr< SelectorOptions< T > > _selectorOptions(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities, const std::function< std::shared_ptr< Node >(std::shared_ptr< T >)> &entity2node)
Definition selector.h:532