Skip to content

Commit 3fcafd6

Browse files
committed
Avoid allocating strings for reference mark default tags
1 parent b3a7b5e commit 3fcafd6

File tree

7 files changed

+210
-152
lines changed

7 files changed

+210
-152
lines changed

src/celengine/axisarrow.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "timeline.h"
3030
#include "timelinephase.h"
3131

32+
using namespace std::string_view_literals;
33+
3234
using celestia::render::LineRenderer;
3335
namespace gl = celestia::gl;
3436
namespace math = celestia::math;
@@ -170,10 +172,7 @@ GetArrowVAO()
170172
/****** ArrowReferenceMark base class ******/
171173

172174
ArrowReferenceMark::ArrowReferenceMark(const Body& _body) :
173-
body(_body),
174-
size(1.0),
175-
color(1.0f, 1.0f, 1.0f),
176-
opacity(1.0f)
175+
body(_body)
177176
{
178177
shadprop.texUsage = TexUsage::VertexColors;
179178
shadprop.lightModel = LightingModel::UnlitModel;
@@ -214,15 +213,8 @@ ArrowReferenceMark::render(Renderer* renderer,
214213

215214
Renderer::PipelineState ps;
216215
ps.depthTest = true;
217-
if (opacity == 1.0f)
218-
{
219-
ps.depthMask = true;
220-
}
221-
else
222-
{
223-
ps.blending = true;
224-
ps.blendFunc = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
225-
}
216+
ps.depthMask = true;
217+
226218
renderer->setPipelineState(ps);
227219

228220
Eigen::Affine3f transform = Eigen::Translation3f(position) * q.cast<float>() * Eigen::Scaling(size);
@@ -235,7 +227,7 @@ ArrowReferenceMark::render(Renderer* renderer,
235227
prog->setMVPMatrices(*m.projection, mv);
236228

237229
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex,
238-
color.red(), color.green(), color.blue(), opacity);
230+
color.red(), color.green(), color.blue(), 1.0f);
239231

240232
GetArrowVAO().draw();
241233
}
@@ -244,29 +236,24 @@ ArrowReferenceMark::render(Renderer* renderer,
244236
/****** AxesReferenceMark base class ******/
245237

246238
AxesReferenceMark::AxesReferenceMark(const Body& _body) :
247-
body(_body),
248-
size(),
249-
opacity(1.0f)
239+
body(_body)
250240
{
251241
shadprop.texUsage = TexUsage::VertexColors;
252242
shadprop.lightModel = LightingModel::UnlitModel;
253243
}
254244

255-
256245
void
257246
AxesReferenceMark::setSize(float _size)
258247
{
259248
size = _size;
260249
}
261250

262-
263251
void
264252
AxesReferenceMark::setOpacity(float _opacity)
265253
{
266254
opacity = _opacity;
267255
}
268256

269-
270257
void
271258
AxesReferenceMark::render(Renderer* renderer,
272259
const Eigen::Vector3f& position,
@@ -293,25 +280,6 @@ AxesReferenceMark::render(Renderer* renderer,
293280
Eigen::Matrix4f projection = *m.projection;
294281
Eigen::Matrix4f modelView = (*m.modelview) * transform.matrix();
295282

296-
#if 0
297-
// Simple line axes
298-
glBegin(GL_LINES);
299-
300-
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
301-
glVertex3f(0.0f, 0.0f, 0.0f);
302-
glVertex3f(-1.0f, 0.0f, 0.0f);
303-
304-
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
305-
glVertex3f(0.0f, 0.0f, 0.0f);
306-
glVertex3f(0.0f, 0.0f, 1.0f);
307-
308-
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
309-
glVertex3f(0.0f, 0.0f, 0.0f);
310-
glVertex3f(0.0f, 1.0f, 0.0f);
311-
312-
glEnd();
313-
#endif
314-
315283
float labelScale = 0.1f;
316284

317285
CelestiaGLProgram* prog = renderer->getShaderManager().getShader(shadprop);
@@ -374,7 +342,6 @@ AxesReferenceMark::render(Renderer* renderer,
374342
VelocityVectorArrow::VelocityVectorArrow(const Body& _body) :
375343
ArrowReferenceMark(_body)
376344
{
377-
setTag("velocity vector");
378345
setColor(Color(0.6f, 0.6f, 0.9f));
379346
setSize(body.getRadius() * 2.0f);
380347
}
@@ -386,13 +353,18 @@ VelocityVectorArrow::getDirection(double tdb) const
386353
return phase->orbitFrame()->getOrientation(tdb).conjugate() * phase->orbit()->velocityAtTime(tdb);
387354
}
388355

356+
std::string_view
357+
VelocityVectorArrow::defaultTag() const
358+
{
359+
return "velocity vector"sv;
360+
}
361+
389362

390363
/****** SunDirectionArrow implementation ******/
391364

392365
SunDirectionArrow::SunDirectionArrow(const Body& _body) :
393366
ArrowReferenceMark(_body)
394367
{
395-
setTag("sun direction");
396368
setColor(Color(1.0f, 1.0f, 0.4f));
397369
setSize(body.getRadius() * 2.0f);
398370
}
@@ -416,13 +388,18 @@ SunDirectionArrow::getDirection(double tdb) const
416388
return Eigen::Vector3d::Zero();
417389
}
418390

391+
std::string_view
392+
SunDirectionArrow::defaultTag() const
393+
{
394+
return "sun direction"sv;
395+
}
396+
419397

420398
/****** SpinVectorArrow implementation ******/
421399

422400
SpinVectorArrow::SpinVectorArrow(const Body& _body) :
423401
ArrowReferenceMark(_body)
424402
{
425-
setTag("spin vector");
426403
setColor(Color(0.6f, 0.6f, 0.6f));
427404
setSize(body.getRadius() * 2.0f);
428405
}
@@ -434,6 +411,12 @@ SpinVectorArrow::getDirection(double tdb) const
434411
return phase->bodyFrame()->getOrientation(tdb).conjugate() * phase->rotationModel()->angularVelocityAtTime(tdb);
435412
}
436413

414+
std::string_view
415+
SpinVectorArrow::defaultTag() const
416+
{
417+
return "spin vector"sv;
418+
}
419+
437420

438421
/****** BodyToBodyDirectionArrow implementation ******/
439422

@@ -444,25 +427,28 @@ BodyToBodyDirectionArrow::BodyToBodyDirectionArrow(const Body& _body, const Sele
444427
ArrowReferenceMark(_body),
445428
target(_target)
446429
{
447-
setTag("body to body");
448430
setColor(Color(0.0f, 0.5f, 0.0f));
449431
setSize(body.getRadius() * 2.0f);
450432
}
451433

452-
453434
Eigen::Vector3d
454435
BodyToBodyDirectionArrow::getDirection(double tdb) const
455436
{
456437
return target.getPosition(tdb).offsetFromKm(body.getPosition(tdb));
457438
}
458439

440+
std::string_view
441+
BodyToBodyDirectionArrow::defaultTag() const
442+
{
443+
return "body to body"sv;
444+
}
445+
459446

460447
/****** BodyAxisArrows implementation ******/
461448

462449
BodyAxisArrows::BodyAxisArrows(const Body& _body) :
463450
AxesReferenceMark(_body)
464451
{
465-
setTag("body axes");
466452
setOpacity(1.0);
467453
setSize(body.getRadius() * 2.0f);
468454
}
@@ -473,13 +459,18 @@ BodyAxisArrows::getOrientation(double tdb) const
473459
return (math::YRot180<double> * body.getEclipticToBodyFixed(tdb)).conjugate();
474460
}
475461

462+
std::string_view
463+
BodyAxisArrows::defaultTag() const
464+
{
465+
return "body axes"sv;
466+
}
467+
476468

477469
/****** FrameAxisArrows implementation ******/
478470

479471
FrameAxisArrows::FrameAxisArrows(const Body& _body) :
480472
AxesReferenceMark(_body)
481473
{
482-
setTag("frame axes");
483474
setOpacity(0.5);
484475
setSize(body.getRadius() * 2.0f);
485476
}
@@ -489,3 +480,9 @@ FrameAxisArrows::getOrientation(double tdb) const
489480
{
490481
return body.getEclipticToFrame(tdb).conjugate();
491482
}
483+
484+
std::string_view
485+
FrameAxisArrows::defaultTag() const
486+
{
487+
return "frame axes"sv;
488+
}

src/celengine/axisarrow.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
#pragma once
1212

13+
#include <string_view>
14+
15+
#include <Eigen/Core>
16+
1317
#include <celutil/color.h>
1418
#include <celengine/referencemark.h>
1519
#include <celengine/selection.h>
@@ -37,20 +41,14 @@ class ArrowReferenceMark : public ReferenceMark
3741
return size;
3842
}
3943

40-
bool isOpaque() const override
41-
{
42-
return opacity == 1.0f;
43-
}
44-
4544
virtual Eigen::Vector3d getDirection(double tdb) const = 0;
4645

4746
protected:
4847
const Body& body;
4948

5049
private:
51-
float size;
52-
Color color;
53-
float opacity;
50+
float size{ 1.0f };
51+
Color color{ 1.0f, 1.0f, 1.0f };
5452
ShaderProperties shadprop;
5553
};
5654

@@ -83,8 +81,8 @@ class AxesReferenceMark : public ReferenceMark
8381
const Body& body;
8482

8583
private:
86-
float size;
87-
float opacity;
84+
float size{ 0.0f };
85+
float opacity{ 1.0f };
8886
ShaderProperties shadprop;
8987
};
9088

@@ -94,6 +92,9 @@ class BodyAxisArrows : public AxesReferenceMark
9492
public:
9593
explicit BodyAxisArrows(const Body& _body);
9694
Eigen::Quaterniond getOrientation(double tdb) const override;
95+
96+
protected:
97+
std::string_view defaultTag() const override;
9798
};
9899

99100

@@ -102,6 +103,9 @@ class FrameAxisArrows : public AxesReferenceMark
102103
public:
103104
explicit FrameAxisArrows(const Body& _body);
104105
Eigen::Quaterniond getOrientation(double tdb) const override;
106+
107+
protected:
108+
std::string_view defaultTag() const override;
105109
};
106110

107111

@@ -110,6 +114,9 @@ class SunDirectionArrow : public ArrowReferenceMark
110114
public:
111115
explicit SunDirectionArrow(const Body& _body);
112116
Eigen::Vector3d getDirection(double tdb) const override;
117+
118+
protected:
119+
std::string_view defaultTag() const override;
113120
};
114121

115122

@@ -118,6 +125,9 @@ class VelocityVectorArrow : public ArrowReferenceMark
118125
public:
119126
explicit VelocityVectorArrow(const Body& _body);
120127
Eigen::Vector3d getDirection(double tdb) const override;
128+
129+
protected:
130+
std::string_view defaultTag() const override;
121131
};
122132

123133

@@ -126,6 +136,9 @@ class SpinVectorArrow : public ArrowReferenceMark
126136
public:
127137
explicit SpinVectorArrow(const Body& _body);
128138
Eigen::Vector3d getDirection(double tdb) const override;
139+
140+
protected:
141+
std::string_view defaultTag() const override;
129142
};
130143

131144

@@ -138,6 +151,9 @@ class BodyToBodyDirectionArrow : public ArrowReferenceMark
138151
BodyToBodyDirectionArrow(const Body& _body, const Selection& _target);
139152
Eigen::Vector3d getDirection(double tdb) const override;
140153

154+
protected:
155+
std::string_view defaultTag() const override;
156+
141157
private:
142158
Selection target;
143159
};

0 commit comments

Comments
 (0)