Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
TrackPainter.h
1// trax track library
2// AD 2013
3//
4// "I'm not the kind of girl who gives up just like that."
5//
6// Blondie
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#include "SectionTrack.h"
30#include "Section.h"
31
32#include "common/NarrowCast.h"
33#include "common/Interval.h"
34#include "spat/Position.h"
35#include "spat/VectorBundle.h"
36#include "spat/VectorBundle2.h"
37#include "spat/Frame.h"
38
39#include <vector>
40
41namespace trax{
42
44 class TrackPainter{
45 public:
58
59
71 dclspc TrackPainter() noexcept;
72 dclspc TrackPainter( int mode, common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length}, Length e = epsilon__length );
73 dclspc TrackPainter( const TrackPainter& ) = default;
74 dclspc TrackPainter( TrackPainter&& ) = default;
76
77
78 dclspc virtual ~TrackPainter() = default;
79
80 TrackPainter& operator=( const TrackPainter& ) = default;
81 TrackPainter& operator=( TrackPainter&& ) = default;
82
83 inline TrackPainter& operator()( const SectionTrack& track ){
84 if( track.IsValid() )
85 Paint( track, *track.GetSection() );
86 return *this;
87 }
88
90 inline int GetMode() const noexcept{
91 return m_Mode;
92 }
93
95 inline int GetCountSegments() const noexcept{
96 return m_CountSegmentsLastPaint;
97 }
98
108 dclspc Length Paint( const TrackBuilder& track, const Section& section, Length offset = 0_m );
110 protected:
112 virtual void StartPaint( const spat::Frame<Length,One>& /*rFrame*/, Length /*offset*/, const Section& /*section*/ ){}
113
117 virtual void PaintSegment( const spat::Frame<Length,One>& rFrame, Length segmentLength, const Section& /*section*/ ) = 0;
118
120 virtual void EndPaint( const spat::Frame<Length,One>& /*rFrame*/ ){}
121 private:
122 int m_Mode;
123 Length m_Epsilon;
124 common::Interval<Length> m_SegmentLimits;
125
126 int m_CountSegmentsLastPaint = 0;
127 spat::Frame<Length,One> m_Transformation;
128 };
129
130
132 class SectionPainter : public TrackPainter{
133 public:
134 dclspc SectionPainter() noexcept;
135 dclspc SectionPainter( int mode, common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length}, Length e = epsilon__length );
136 protected:
137 void dclspc StartPaint( const spat::Frame<Length,One>& rFrame, Length offset, const Section& section ) override;
138 void dclspc PaintSegment( const spat::Frame<Length,One>& rFrame, Length segmentLength, const Section& section ) override;
139 virtual void AddVertex( const spat::Frame<Length,One>& rFrame, const Section::SectionPoint& pt, const spat::Position2D<One>& textcoord ) = 0;
140 virtual void AddTriangle( unsigned int i0, unsigned int i1, unsigned int i2 ) = 0;
141 private:
142 void AddVertexRowAt( const spat::Frame<Length,One>& rFrame, const Section& section );
143 int m_NextIndex;
144 Value<Dimension<-1,0,0>> m_TexScaleU;
145 One m_TexCoordU;
146 };
147
149 template<typename PointType_>
150 class BufferedPainter_Base : public SectionPainter{
151 public:
152 using PointType = PointType_;
153
154 BufferedPainter_Base( std::vector<PointType>& points,
155 std::vector<int>& indices,
156 int mode = TrackPainter::Mode::mode_default,
157 common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length},
158 Length e = epsilon__length )
159 : SectionPainter {mode,segmentLimits,e},
160 m_Points {points},
161 m_Indices {indices},
162 m_IndexOffset {common::narrow_cast<int>(m_Points.size())}
163 {
164 }
165
166 protected:
167 void StartPaint( const spat::Frame<Length,One>& rFrame, Length offset, const Section& section ) override{
168 m_IndexOffset = common::narrow_cast<int>(m_Points.size());
169 SectionPainter::StartPaint( rFrame, offset, section );
170 }
171
172 void AddTriangle( unsigned int i0, unsigned int i1, unsigned int i2 ) override{
173 m_Indices.push_back( m_IndexOffset + i0 );
174 m_Indices.push_back( m_IndexOffset + i1 );
175 m_Indices.push_back( m_IndexOffset + i2 );
176 }
177
178 std::vector<PointType>& m_Points;
179 private:
180 std::vector<int>& m_Indices;
181 int m_IndexOffset;
182 };
183
184
185 template<typename PointType>
186 class BufferedPainter : public BufferedPainter_Base<PointType>{
187 public:
188 BufferedPainter( std::vector<PointType>& points,
189 std::vector<int>& indices,
190 int mode = TrackPainter::Mode::mode_default,
191 common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length},
192 Length e = epsilon__length )
193 : BufferedPainter_Base<PointType>{points,indices,mode,segmentLimits,e}
194 {
195 }
196
197 protected:
198 };
199
200
201 template<>
202 class BufferedPainter<spat::Position<Length>> : public BufferedPainter_Base<spat::Position<Length>>{
203 public:
204 BufferedPainter( std::vector<PointType>& points,
205 std::vector<int>& indices,
206 int mode = TrackPainter::Mode::mode_default,
207 common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length},
208 Length e = epsilon__length )
209 : BufferedPainter_Base<PointType>{points,indices,mode,segmentLimits,e}
210 {
211 }
212
213 protected:
214 void AddVertex( const spat::Frame<Length,One>& rFrame, const Section::SectionPoint& pt, const spat::Position2D<One>& /*textcoord*/ ) override{
215 m_Points.push_back( rFrame.P + pt.p.x * rFrame.N + pt.p.y * rFrame.B );
216 }
217 };
218
219 template<>
220 class BufferedPainter<std::pair<spat::VectorBundle<Length,One>,spat::Position2D<One>>> : public BufferedPainter_Base<std::pair<spat::VectorBundle<Length,One>,spat::Position2D<One>>>{
221 public:
222 BufferedPainter( std::vector<PointType>& points,
223 std::vector<int>& indices,
224 int mode = TrackPainter::Mode::mode_default,
225 common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length},
226 Length e = epsilon__length )
227 : BufferedPainter_Base<PointType>{points,indices,mode,segmentLimits,e}
228 {
229 }
230
231 protected:
232 void AddVertex( const spat::Frame<Length,One>& rFrame, const Section::SectionPoint& pt, const spat::Position2D<One>& textcoord ) override{
233 m_Points.push_back( {{ rFrame.P + pt.p.x * rFrame.N + pt.p.y * rFrame.B,
234 pt.n.dx * rFrame.N + pt.n.dy * rFrame.B },
235 textcoord} );
236 }
237 };
238
239 template<>
240 class BufferedPainter<std::pair<spat::VectorBundle2<Length,One>,spat::Position2D<One>>> : public BufferedPainter_Base<std::pair<spat::VectorBundle2<Length,One>,spat::Position2D<One>>>{
241 public:
242 BufferedPainter( std::vector<PointType>& points,
243 std::vector<int>& indices,
244 int mode = TrackPainter::Mode::mode_default,
245 common::Interval<Length> segmentLimits = {epsilon__length,plausible_maximum_length},
246 Length e = epsilon__length )
247 : BufferedPainter_Base<PointType>{points,indices,mode,segmentLimits,e}
248 {
249 }
250
251 protected:
252 void AddVertex( const spat::Frame<Length,One>& rFrame, const Section::SectionPoint& pt, const spat::Position2D<One>& textcoord ) override{
253 m_Points.push_back( {{ rFrame.P + pt.p.x * rFrame.N + pt.p.y * rFrame.B,
254 rFrame.T,
255 pt.n.dx * rFrame.N + pt.n.dy * rFrame.B },
256 textcoord} );
257 }
258 };
259}
Value type, dependend from dimensions.
Definition DimensionedValues.h:233
void StartPaint(const spat::Frame< Length, One > &rFrame, Length offset, const Section &section) override
Gets called once on rendering startup before all PaintSegment() calls.
Definition TrackPainter.h:167
void dclspc StartPaint(const spat::Frame< Length, One > &rFrame, Length offset, const Section &section) override
Gets called once on rendering startup before all PaintSegment() calls.
void dclspc PaintSegment(const spat::Frame< Length, One > &rFrame, Length segmentLength, const Section &section) override
This gets called once per segment to paint a segment of the track.
Base class for painting tracks as a series of short straight pieces.
Definition TrackPainter.h:44
dclspc Length Paint(const TrackBuilder &track, const Section &section, Length offset=0_m)
Paints the track.
int GetMode() const noexcept
Definition TrackPainter.h:90
virtual void PaintSegment(const spat::Frame< Length, One > &rFrame, Length segmentLength, const Section &)=0
This gets called once per segment to paint a segment of the track.
int GetCountSegments() const noexcept
Definition TrackPainter.h:95
virtual void EndPaint(const spat::Frame< Length, One > &)
Gets called once on rendering end after all PaintSegment() calls.
Definition TrackPainter.h:120
Mode
Definition TrackPainter.h:46
@ mode_startFrame
Paints the track around it's start frame. The first segment will start at {0,0,0} with it's tangent h...
Definition TrackPainter.h:55
@ mode_leftHandedFaces
Produces triangles according to the left hand rule. Default it is the right hand rule.
Definition TrackPainter.h:48
@ mode_SegmentLength
Checks each segment at its starting point. The default.
Definition TrackPainter.h:50
@ mode_constantSegmentLength
Uses segmentLimits.Far() as segment length.
Definition TrackPainter.h:49
@ mode_ignoreCuvesTorsion
Ignores the curves torsion (but not the track's twist) for segment calculations.
Definition TrackPainter.h:53
@ mode_checkedSegmentLength
Checks the end point of the segment, shrinks if necessary. Use it if the curvature changes much insid...
Definition TrackPainter.h:51
@ mode_localFrame
Paints the track in it's local frame of reference. To get global coordinates, the tracks's transforma...
Definition TrackPainter.h:54
@ mode_totallycheckedSegmentLength
Checks every point of the segment with respect to the segmentLimits.Near() grid. Use it if sudden cur...
Definition TrackPainter.h:52
virtual void StartPaint(const spat::Frame< Length, One > &, Length, const Section &)
Gets called once on rendering startup before all PaintSegment() calls.
Definition TrackPainter.h:112
Namespace of common utility classes and methods.
Definition Helpers.h:43
Target narrow_cast(Source v)
Safe cast for casting numeric values.
Definition NarrowCast.h:60
Value< Dimension< 0, 0, 0 > > One
Dimensionless value.
Definition DimensionedValues.h:319
Value< Dimension< 1, 0, 0 > > Length
Length.
Definition DimensionedValues.h:324
constexpr Real _m(Length l) noexcept
Dimensionated Values conversion functions.
Definition DimensionedValues.h:1210
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
STL namespace.
Namespace of all the trax track libraries classes and methods.
Definition Collection.h:17
An interval describes the area between two numbers. It is understood to contain the near one and exlu...
Definition Interval.h:42
Type selection for class Value.
Definition DimensionedValues.h:224
A Frame ("TNBFrame") describes a location in 3d space and an orientation using a right handed coordin...
Definition Frame.h:52
Vector< ValtypeT > B
Binormal axis or z-axis.
Definition Frame.h:56
Position< Valtype > P
Point in 3D space.
Definition Frame.h:53
Vector< ValtypeT > N
Normal axis or y-axis.
Definition Frame.h:55
Vector< ValtypeT > T
Tangential axis or x-axis.
Definition Frame.h:54
Implements a 2D - position in cartesian coordinates.
Definition Position2D.h:45
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Point structure used to describe a section.
Definition Section.h:82
spat::Position2D< Length > p
Position of profile point.
Definition Section.h:83
spat::Vector2D< One > n
Normal of profile point.
Definition Section.h:84
A profile of a track.
Definition Section.h:45
Interface for making and shaping tracks.
Definition Track.h:831