Cumulia Illustrator Rendering Engine v2.1.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 2025-08-05
15// Version V2.1.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
60 {
61 public:
63 {
64 }
65
66 virtual ~Selector()
67 {
68 }
69 };
70
71 template <typename T>
72 class BaseSelector : public Selector
73 {
74 public:
76
77 {
78 }
79
80 virtual ~BaseSelector()
81 {
82 }
83
84 virtual void update(SelectorUpdateMode mode) = 0;
85 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities) = 0;
86 };
87
88 template <typename T>
89 class EntitySelector : public BaseSelector<T>
90 {
91 protected:
92 std::shared_ptr<EntityBuilder<T>> m_builder;
93 std::shared_ptr<EntityProducer> m_producer;
95
96 private:
97 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
98 std::shared_ptr<Image2D> m_bufferImage;
99
100 public:
102 {
103 }
104
106 {
107 }
108
109 virtual void update(SelectorUpdateMode mode)
110 {
111 switch (mode)
112 {
114 this->m_bufferImage = nullptr;
115 break;
118 {
119 this->m_builtNodes = nullptr;
120 this->m_bufferImage = nullptr;
121 break;
122 }
123 default: break;
124 }
125 }
126
127 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
128 {
129 this->m_builder->setEntities(entities);
131 }
132
133 std::shared_ptr<T> select(int x, int y)
134 {
135 this->buildNodes();
136 if (this->m_buffered)
137 {
138 this->produceBufferImage();
139 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
140 auto pixelIndex = consumer->select(x, y);
141 unsigned int entityIndex = ((unsigned int*)this->m_bufferImage->data())[pixelIndex];
142 return entityIndex != 0XFFFFFFFF ? (*this->m_builder->entities())[entityIndex] : nullptr;
143 }
144 else
145 {
146 this->m_producer->toPixel(x, y);
147 auto image = this->m_producer->produce(this->m_builtNodes);
148 unsigned int entityIndex = ((unsigned int*)image->data())[0];
149 return entityIndex != 0XFFFFFFFF ? (*this->m_builder->entities())[entityIndex] : nullptr;
150 }
151 }
152
153 std::vector<std::shared_ptr<T>> multiSelect(int x1, int y1, int x2, int y2)
154 {
155 this->buildNodes();
156 if (this->m_buffered)
157 {
158 this->produceBufferImage();
159 auto consumer = std::make_shared<BoxConsumer>(this->m_producer->windowSize());
160 std::vector<int> pixelIndices = consumer->select(x1, y1, x2, y2);
161
162 std::set<unsigned int> entityIndices;
163 auto imageData = (unsigned int*)this->m_bufferImage->data();
164 for (auto pixelIndex : pixelIndices)
165 {
166 entityIndices.insert(imageData[pixelIndex]);
167 }
168 entityIndices.erase(0XFFFFFFFF);
169
170 std::vector<std::shared_ptr<T>> result;
171 auto& entities = *this->m_builder->entities();
172 for (auto entityIndex : entityIndices)
173 {
174 result.push_back(entities[entityIndex]);
175 }
176
177 return result;
178 }
179 else
180 {
181 this->m_producer->toBox(x1, y1, x2, y2);
182 auto image = this->m_producer->produce(this->m_builtNodes);
183
184 std::set<unsigned int> entityIndices;
185 auto imageData = (unsigned int*)image->data();
186 auto length = image->width() * image->height();
187 for (int i = 0; i < length; ++i)
188 {
189 entityIndices.insert(imageData[i]);
190 }
191 entityIndices.erase(0XFFFFFFFF);
192
193 std::vector<std::shared_ptr<T>> result;
194 auto& entities = *this->m_builder->entities();
195 for (auto entityIndex : entityIndices)
196 {
197 result.push_back(entities[entityIndex]);
198 }
199
200 return result;
201 }
202 }
203
204 private:
205 void buildNodes()
206 {
207 if (!this->m_builtNodes)
208 {
209 this->m_builtNodes = this->m_builder->build();
210 this->m_producer->updatePass();
211 }
212 }
213
214 void produceBufferImage()
215 {
216 if (!this->m_bufferImage) {
217 this->m_producer->toWindow();
218 this->m_bufferImage = this->m_producer->produce(this->m_builtNodes);
219 }
220 }
221 };
222
223 template <typename T>
224 class FaceSelector : public EntitySelector<T>
225 {
226 public:
227 FaceSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
228 : EntitySelector<T>()
229 {
230 this->m_builder = std::make_shared<FaceBuilder<T>>(options->entities, options->entity2node);
231 this->m_producer = std::make_shared<FaceProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
232 this->m_buffered = buffered;
233 }
234
236 {
237 }
238 };
239
240 template <typename T>
242 {
243 public:
244 VertexSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
245 : EntitySelector<T>()
246 {
247 this->m_builder = std::make_shared<VertexBuilder<T>>(options->entities, options->entity2node);
248 this->m_producer = std::make_shared<VertexProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
249 this->m_buffered = buffered;
250 }
251
253 {
254 }
255 };
256
257 template <typename T>
258 class EdgeSelector : public EntitySelector<T>
259 {
260 public:
261 EdgeSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
262 : EntitySelector<T>()
263 {
264 this->m_builder = std::make_shared<EdgeBuilder<T>>(options->entities, options->entity2node);
265 this->m_producer = std::make_shared<EdgeProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
266 this->m_buffered = buffered;
267 }
268
270 {
271 }
272 };
273
274 template <typename T>
276 {
277 protected:
278 std::shared_ptr<AttributeBuilder<T>> m_builder;
279 std::shared_ptr<AttributeProducer> m_producer;
281
282 private:
283 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
284 std::vector<std::shared_ptr<Image2D>> m_bufferImages;
285
286 public:
288 : BaseSelector<T>()
289 {
290 }
291
293 {
294 }
295
296 virtual void update(SelectorUpdateMode mode)
297 {
298 switch (mode)
299 {
301 this->m_bufferImages.clear();
302 break;
305 {
306 this->m_builtNodes = nullptr;
307 this->m_bufferImages.clear();
308 break;
309 }
310 default: break;
311 }
312 }
313
314 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
315 {
316 this->m_builder->setEntities(entities);
318 }
319
320 std::shared_ptr<Vector3f> select(int x, int y)
321 {
322 this->buildNodes();
323 if (this->m_buffered)
324 {
325 this->produceBufferImages();
326 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
327 auto pixelIndex = consumer->select(x, y);
328 unsigned int value = ((unsigned int*)this->m_bufferImages[0]->data())[pixelIndex];
329 if (value != 0XFFFFFFFF)
330 {
331 auto x = ((float*)this->m_bufferImages[0]->data())[pixelIndex];
332 auto y = ((float*)this->m_bufferImages[1]->data())[pixelIndex];
333 auto z = ((float*)this->m_bufferImages[2]->data())[pixelIndex];
334
335 return _vec3(x, y, z);
336 }
337 else
338 {
339 return nullptr;
340 }
341 }
342 else
343 {
344 this->m_producer->toPixel(x, y);
345 auto images = this->m_producer->produce(this->m_builtNodes);
346 unsigned int value = ((unsigned int*)images[0]->data())[0];
347 if (value != 0XFFFFFFFF)
348 {
349 auto x = ((float*)images[0]->data())[0];
350 auto y = ((float*)images[1]->data())[0];
351 auto z = ((float*)images[2]->data())[0];
352
353 return _vec3(x, y, z);
354 }
355 else
356 {
357 return nullptr;
358 }
359 }
360 }
361
362 private:
363 void buildNodes()
364 {
365 if (!this->m_builtNodes)
366 {
367 this->m_builtNodes = this->m_builder->build();
368 this->m_producer->updatePass();
369 }
370 }
371
372 void produceBufferImages()
373 {
374 if (this->m_bufferImages.size() == 0) {
375 this->m_producer->toWindow();
376 this->m_bufferImages = this->m_producer->produce(this->m_builtNodes);
377 }
378 }
379 };
380
381 template <typename T>
383 {
384 public:
385 PositionSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
386 : AttributeSelector<T>()
387 {
388 this->m_builder = std::make_shared<PositionBuilder<T>>(options->entities, options->entity2node);
389 this->m_producer = std::make_shared<PositionProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
390 this->m_buffered = buffered;
391 }
392
394 {
395 }
396 };
397
398 template <typename T>
400 {
401 public:
402 NormalSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
403 : AttributeSelector<T>()
404 {
405 this->m_builder = std::make_shared<NormalBuilder<T>>(options->entities, options->entity2node);
406 this->m_producer = std::make_shared<NormalProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
407 this->m_buffered = buffered;
408 }
409
411 {
412 }
413 };
414
415 template <typename T>
417 {
418 protected:
419 std::shared_ptr<PrimitiveBuilder<T>> m_builder;
420 std::shared_ptr<PrimitiveProducer> m_producer;
422
423 private:
424 std::shared_ptr<std::vector<std::shared_ptr<Node>>> m_builtNodes;
425 std::shared_ptr<Image2D> m_bufferImage;
426
427 public:
428 PrimitiveSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
429 : BaseSelector<T>()
430 {
431 this->m_builder = std::make_shared<PrimitiveBuilder<T>>(options->entities, options->entity2node);
432 this->m_producer = std::make_shared<PrimitiveProducer>(engine->renderer, engine->camera, engine->backgroundSize, engine->windowSize);
433 this->m_buffered = buffered;
434 }
435
437 {
438 }
439
440 virtual void update(SelectorUpdateMode mode)
441 {
442 switch (mode)
443 {
445 this->m_bufferImage = nullptr;
446 break;
449 {
450 this->m_builtNodes = nullptr;
451 this->m_bufferImage = nullptr;
452 break;
453 }
454 default: break;
455 }
456 }
457
458 virtual void setEntities(const std::shared_ptr<std::vector<std::shared_ptr<T>>>& entities)
459 {
460 this->m_builder->setEntities(entities);
462 }
463
464 unsigned int select(int x, int y)
465 {
466 this->buildNodes();
467 if (this->m_buffered)
468 {
469 this->produceBufferImage();
470 auto consumer = std::make_shared<PixelConsumer>(this->m_producer->windowSize());
471 auto pixelIndex = consumer->select(x, y);
472 unsigned int primitiveIndex = ((unsigned int*)this->m_bufferImage->data())[pixelIndex];
473
474 return primitiveIndex;
475 }
476 else
477 {
478 this->m_producer->toPixel(x, y);
479 auto image = this->m_producer->produce(this->m_builtNodes);
480 unsigned int primitiveIndex = ((unsigned int*)image->data())[0];
481
482 return primitiveIndex;
483 }
484 }
485
486 std::vector<unsigned int> multiSelect(int x1, int y1, int x2, int y2)
487 {
488 this->buildNodes();
489 if (this->m_buffered)
490 {
491 this->produceBufferImage();
492 auto consumer = std::make_shared<BoxConsumer>(this->m_producer->windowSize());
493 std::vector<int> pixelIndices = consumer->select(x1, y1, x2, y2);
494
495 std::set<unsigned int> primitiveIndices;
496 auto imageData = (unsigned int*)this->m_bufferImage->data();
497 for (auto pixelIndex : pixelIndices)
498 {
499 primitiveIndices.insert(imageData[pixelIndex]);
500 }
501 primitiveIndices.erase(0XFFFFFFFF);
502
503 return std::vector<unsigned int>(primitiveIndices.begin(), primitiveIndices.end());
504 }
505 else
506 {
507 this->m_producer->toBox(x1, y1, x2, y2);
508 auto image = this->m_producer->produce(this->m_builtNodes);
509 auto imageData = (unsigned int*)image->data();
510
511 std::set<unsigned int> primitiveIndices;
512 auto length = image->width() * image->height();
513 for (int i = 0; i < length; ++i)
514 {
515 primitiveIndices.insert(imageData[i]);
516 }
517 primitiveIndices.erase(0XFFFFFFFF);
518
519 return std::vector<unsigned int>(primitiveIndices.begin(), primitiveIndices.end());
520 }
521 }
522
523 private:
524 void buildNodes()
525 {
526 if (!this->m_builtNodes)
527 {
528 this->m_builtNodes = this->m_builder->build();
529 this->m_producer->updatePass();
530 }
531 }
532
533 void produceBufferImage()
534 {
535 if (!this->m_bufferImage) {
536 this->m_producer->toWindow();
537 this->m_bufferImage = this->m_producer->produce(this->m_builtNodes);
538 }
539 }
540 };
541
542 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);
543
544 template <typename T>
545 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)
546 {
547 auto options = std::make_shared<SelectorOptions<T>>();
548 options->entities = entities;
549 options->entity2node = entity2node;
550
551 return options;
552 }
553
554 template <typename T>
555 std::shared_ptr<FaceSelector<T>> _faceSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
556 {
557 return std::make_shared<FaceSelector<T>>(engine, options, buffered);
558 }
559
560 template <typename T>
561 std::shared_ptr<VertexSelector<T>> _vertexSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
562 {
563 return std::make_shared<VertexSelector<T>>(engine, options, buffered);
564 }
565
566 template <typename T>
567 std::shared_ptr<EdgeSelector<T>> _edgeSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = true)
568 {
569 return std::make_shared<EdgeSelector<T>>(engine, options, buffered);
570 }
571
572 template <typename T>
573 std::shared_ptr<PositionSelector<T>> _positionSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
574 {
575 return std::make_shared<PositionSelector<T>>(engine, options, buffered);
576 }
577
578 template <typename T>
579 std::shared_ptr<NormalSelector<T>> _normalSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
580 {
581 return std::make_shared<NormalSelector<T>>(engine, options, buffered);
582 }
583
584 template <typename T>
585 std::shared_ptr<PrimitiveSelector<T>> _primitiveSelector(const std::shared_ptr<SelectorEngine>& engine, const std::shared_ptr<SelectorOptions<T>>& options, bool buffered = false)
586 {
587 return std::make_shared<PrimitiveSelector<T>>(engine, options, buffered);
588 }
589}
Definition selector.h:276
virtual void update(SelectorUpdateMode mode)
Definition selector.h:296
AttributeSelector()
Definition selector.h:287
std::shared_ptr< AttributeBuilder< T > > m_builder
Definition selector.h:278
std::shared_ptr< AttributeProducer > m_producer
Definition selector.h:279
virtual ~AttributeSelector()
Definition selector.h:292
bool m_buffered
Definition selector.h:280
std::shared_ptr< Vector3f > select(int x, int y)
Definition selector.h:320
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:314
Definition selector.h:73
BaseSelector()
Definition selector.h:75
virtual ~BaseSelector()
Definition selector.h:80
virtual void update(SelectorUpdateMode mode)=0
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)=0
Definition selector.h:259
virtual ~EdgeSelector()
Definition selector.h:269
EdgeSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:261
Definition selector.h:90
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:127
std::shared_ptr< EntityBuilder< T > > m_builder
Definition selector.h:92
EntitySelector()
Definition selector.h:101
std::vector< std::shared_ptr< T > > multiSelect(int x1, int y1, int x2, int y2)
Definition selector.h:153
std::shared_ptr< EntityProducer > m_producer
Definition selector.h:93
virtual ~EntitySelector()
Definition selector.h:105
bool m_buffered
Definition selector.h:94
std::shared_ptr< T > select(int x, int y)
Definition selector.h:133
virtual void update(SelectorUpdateMode mode)
Definition selector.h:109
Definition selector.h:225
FaceSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:227
virtual ~FaceSelector()
Definition selector.h:235
Definition selector.h:400
NormalSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:402
virtual ~NormalSelector()
Definition selector.h:410
Definition selector.h:383
virtual ~PositionSelector()
Definition selector.h:393
PositionSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:385
Definition selector.h:417
unsigned int select(int x, int y)
Definition selector.h:464
bool m_buffered
Definition selector.h:421
std::vector< unsigned int > multiSelect(int x1, int y1, int x2, int y2)
Definition selector.h:486
virtual void update(SelectorUpdateMode mode)
Definition selector.h:440
PrimitiveSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=false)
Definition selector.h:428
std::shared_ptr< PrimitiveBuilder< T > > m_builder
Definition selector.h:419
std::shared_ptr< PrimitiveProducer > m_producer
Definition selector.h:420
virtual ~PrimitiveSelector()
Definition selector.h:436
virtual void setEntities(const std::shared_ptr< std::vector< std::shared_ptr< T > > > &entities)
Definition selector.h:458
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:60
virtual ~Selector()
Definition selector.h:66
Selector()
Definition selector.h:62
Definition selector.h:242
VertexSelector(const std::shared_ptr< SelectorEngine > &engine, const std::shared_ptr< SelectorOptions< T > > &options, bool buffered=true)
Definition selector.h:244
virtual ~VertexSelector()
Definition selector.h:252
Definition decal.h:23
SelectorUpdateMode
Definition selector.h:35
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:561
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:579
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:555
Vector3fPtr _vec3()
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:585
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:567
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:573
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:545