Welcome to mirror list, hosted at ThFree Co, Russian Federation.

line_shape.cpp « drape_frontend - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbf68594275c58dda0f10f6b9ca181df409133f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "line_shape.hpp"

#include "../drape/shader_def.hpp"
#include "../drape/attribute_provider.hpp"
#include "../drape/glstate.hpp"
#include "../drape/batcher.hpp"

#include "../base/math.hpp"
#include "../base/logging.hpp"
#include "../base/stl_add.hpp"

#include "../std/algorithm.hpp"
#include "../std/vector.hpp"

using m2::PointF;

namespace df
{

namespace
{
  static uint32_t const ListStride = 24;
}

/// Split angle v1-v2-v3 by bisector.
void Bisector(float R, PointF const & v1, PointF const & v2, PointF const & v3,
              PointF & leftBisector, PointF & rightBisector, PointF & dx)
{
  PointF const dif21 = v2 - v1;
  PointF const dif32 = v3 - v2;

  float const l1 = dif21.Length();
  float const l2 = dif32.Length();
  float const l3 = (v1 - v3).Length();

  /// normal1 is normal from segment v2 - v1
  /// normal2 is normal from segmeny v3 - v2
  PointF normal1(-dif21.y / l1, dif21.x / l1);
  PointF const normal2(-dif32.y / l2, dif32.x / l2);

  leftBisector = normal1 + normal2;

  /// find cos(2a), where angle a is a half of v1-v2-v3 angle
  float const cos2A = (l1 * l1 + l2 * l2 - l3 * l3) / (2.0f * l1 * l2);
  float const sinA = sqrt((1.0f - cos2A) / 2.0f);
  float const radius = R / sinA;

  leftBisector = (leftBisector * radius) / leftBisector.Length();
  rightBisector = -leftBisector;

  normal1 = normal1 * R;
  dx = leftBisector - normal1;

  float const sign = (m2::DotProduct(dif21, leftBisector) > 0.0f) ? 1.0f : -1.0f;
  dx.x = 2.0f * sign * (dx.Length() / l1);

  leftBisector += v2;
  rightBisector += v2;
}

template <typename T>
void QuadStripToList(vector<T> & dst, vector<T> & src, int32_t index)
{
  static const int32_t dstStride = 6;
  static const int32_t srcStride = 4;
  const int32_t baseDstIndex = index * dstStride;
  const int32_t baseSrcIndex = index * srcStride;

  dst[baseDstIndex] = src[baseSrcIndex];
  dst[baseDstIndex + 1] = src[baseSrcIndex + 1];
  dst[baseDstIndex + 2] = src[baseSrcIndex + 2];
  dst[baseDstIndex + 3] = src[baseSrcIndex + 1];
  dst[baseDstIndex + 4] = src[baseSrcIndex + 3];
  dst[baseDstIndex + 5] = src[baseSrcIndex + 2];
}

void SetColor(vector<float> &dst, float const * ar, int index)
{
  uint32_t const colorArraySize = 4;
  uint32_t const baseListIndex = ListStride * index;
  for (uint32_t i = 0; i < 6; ++i)
    memcpy(&dst[baseListIndex + colorArraySize * i], ar, colorArraySize * sizeof(float));
}

struct Vertex
{
  Vertex() {}
  Vertex(m2::PointF const & pos, m2::PointF const & dir)
    : m_position(pos), m_direction(dir) {}

  Vertex(float posX, float posY, float dirX = 0.0f, float dirY = 0.0f)
    : m_position(posX, posY), m_direction(dirX, dirY) {}

  m2::PointF m_position;
  m2::PointF m_direction;
};

struct Offset
{
  Offset() : m_topOffset(0.0f), m_pointSide(0.0f), m_depth(0.0f) {}
  Offset(float topOffset, float pointSide, float depth)
    : m_topOffset(topOffset), m_pointSide(pointSide), m_depth(depth) {}

  float m_topOffset;
  float m_pointSide;
  float m_depth;
};

struct WidthType
{
  WidthType() {}
  WidthType(float halfWidth, float cap, float join, float insetsWidth)
    : m_halfWidth(halfWidth), m_capType(cap), m_joinType(join), m_insetsWidth(insetsWidth) {}

  float m_halfWidth;
  float m_capType;
  float m_joinType;
  float m_insetsWidth;
};

struct SphereCenters
{
  SphereCenters() {}
  SphereCenters(m2::PointF const & center1, m2::PointF const & center2)
    : m_center1(center1), m_center2(center2) {}

  m2::PointF m_center1;
  m2::PointF m_center2;
};

LineShape::LineShape(vector<m2::PointF> const & points,
                     LineViewParams const & params)
  : m_params(params)
  , m_points(params.m_cap == dp::ButtCap ? points.size() : points.size() + 2)
{
  ASSERT_GREATER(points.size(), 1, ());

  int const size = m_points.size();
  if (m_params.m_cap != dp::ButtCap)
  {
    m_points[0] = points[0] + (points[0] - points[1]).Normalize();;
    m_points[size - 1] = points[size - 3] + (points[size - 3] - points[size - 4]).Normalize();
    memcpy(&m_points[1], &points[0], (size - 2) * sizeof(PointF));
  }
  else
    m_points = points;
}

void LineShape::Draw(RefPointer<Batcher> batcher, RefPointer<TextureSetHolder> /*textures*/) const
{
  int size = m_points.size();
  float const r = 1.0f;

  int const numVert = (size - 1) * 4;
  vector<Vertex> vertex(numVert);
  vector<Offset> dxVals(numVert);
  vector<SphereCenters> centers(numVert);
  vector<WidthType> widthType(numVert);

  PointF leftBisector, rightBisector, dx;

  PointF v2 = m_points[0];
  PointF v3 = m_points[1];
  PointF v1 = v2 * 2 - v3;

  Bisector(r, v1, v2, v3, leftBisector, rightBisector, dx);

  float const joinType = m_params.m_join == dp::RoundJoin ? 1 : 0;
  float const halfWidth = m_params.m_width / 2.0f;
  float const insetHalfWidth= 1.0f * halfWidth;

  vertex[0] = Vertex(v2, leftBisector);
  vertex[1] = Vertex(v2, rightBisector);
  dxVals[0] = Offset(0.0f, -1.0f, m_params.m_depth);
  dxVals[1] = Offset(0.0f, -1.0f, m_params.m_depth);
  centers[0] = centers[1] = SphereCenters(v2, v3);

  widthType[0] = widthType[2] = WidthType(halfWidth, 0, joinType, insetHalfWidth);
  widthType[1] = widthType[3] = WidthType(-halfWidth, 0, joinType, insetHalfWidth);

  //points in the middle
  for(int i = 1 ; i < size - 1 ; ++i)
  {
    v1 = v2;
    v2 = v3;
    v3 = m_points[i + 1];
    Bisector(r, v1, v2, v3, leftBisector, rightBisector, dx);
    float aspect = (v1-v2).Length() / (v2-v3).Length();

    vertex[(i-1) * 4 + 2] = Vertex(v2, leftBisector);
    vertex[(i-1) * 4 + 3] = Vertex(v2, rightBisector);
    dxVals[(i-1) * 4 + 2] = Offset(dx.x, 1.0f, m_params.m_depth);
    dxVals[(i-1) * 4 + 3] = Offset(-dx.x, 1.0f, m_params.m_depth);
    centers[(i-1) * 4 + 2] = centers[(i-1) * 4 + 3] = SphereCenters(v1, v2);

    vertex[i * 4 + 0] = Vertex(v2, leftBisector);
    vertex[i * 4 + 1] = Vertex(v2, rightBisector);
    dxVals[i * 4 + 0] = Offset(-dx.x * aspect, -1.0f, m_params.m_depth);
    dxVals[i * 4 + 1] = Offset(dx.x * aspect, -1.0f, m_params.m_depth);
    centers[i * 4] = centers[i * 4 + 1] = SphereCenters(v2, v3);

    widthType[(i * 4) + 0] = widthType[(i * 4) + 2] = WidthType(halfWidth, 0, joinType, insetHalfWidth);
    widthType[(i * 4) + 1] = widthType[(i * 4) + 3] = WidthType(-halfWidth, 0, joinType, insetHalfWidth);
  }

  //last points
  v1 = v2;
  v2 = v3;
  v3 = v2 * 2 - v1;

  Bisector(r, v1, v2, v3, leftBisector, rightBisector, dx);
  float const aspect = (v1-v2).Length() / (v2-v3).Length();

  vertex[(size - 2) * 4 + 2] = Vertex(v2, leftBisector);
  vertex[(size - 2) * 4 + 3] = Vertex(v2, rightBisector);
  dxVals[(size - 2) * 4 + 2] = Offset(-dx.x * aspect, 1.0f, m_params.m_depth);
  dxVals[(size - 2) * 4 + 3] = Offset(dx.x * aspect, 1.0f, m_params.m_depth);
  widthType[(size - 2) * 4] = widthType[(size - 2) * 4 + 2] = WidthType(halfWidth, 0, joinType, insetHalfWidth);
  widthType[(size - 2) * 4 + 1] = widthType[(size - 2) * 4 + 3] = WidthType(-halfWidth, 0, joinType, insetHalfWidth);
  centers[(size - 2) * 4 + 2] = centers[(size - 2) * 4 + 3] = SphereCenters(v1, v2);

  if (m_params.m_cap != dp::ButtCap)
  {
    float const type = m_params.m_cap == dp::RoundCap ? -1 : 1;
    uint32_t const baseIdx = 4 * (size - 2);
    widthType[0] = WidthType(halfWidth, type, -1, insetHalfWidth);
    widthType[1] = WidthType(-halfWidth, type, -1, insetHalfWidth);
    widthType[2] = WidthType(halfWidth, type, -1, insetHalfWidth);
    widthType[3] = WidthType(-halfWidth, type, -1, insetHalfWidth);

    widthType[baseIdx] = WidthType(halfWidth, type, 1, insetHalfWidth);
    widthType[baseIdx + 1] = WidthType(-halfWidth, type, 1, insetHalfWidth);
    widthType[baseIdx + 2] = WidthType(halfWidth, type, 1, insetHalfWidth);
    widthType[baseIdx + 3] = WidthType(-halfWidth, type, 1, insetHalfWidth);

    vertex[0].m_position = vertex[2].m_position;
    vertex[1].m_position = vertex[3].m_position;
    vertex[baseIdx + 2].m_position = vertex[baseIdx].m_position;
    vertex[baseIdx + 3].m_position = vertex[baseIdx + 1].m_position;
  }

  float clr1[4];
  Convert(m_params.m_color, clr1[0], clr1[1], clr1[2], clr1[3]);
  /// TODO this color now not using. We need merge line styles to draw line outline and line by ont pass
  float const clr2[4] = {0.5f, 0.5f, 0.5f, 1.0f};

  /// TODO add additional functionality in batcher for better perfomance
  int32_t const listVertexCount = (numVert >> 1) * 3;
  vector<Vertex> vertex2(listVertexCount);
  vector<Offset> dxVals2(listVertexCount);
  vector<SphereCenters> centers2(listVertexCount);
  vector<WidthType> widthType2(listVertexCount);
  vector<float> baseColor(numVert * 6);
  vector<float> outlineColor(numVert * 6);
  for(int i = 0; i < size-1 ; i++)
  {
    QuadStripToList(vertex2, vertex, i);
    QuadStripToList(dxVals2, dxVals, i);
    QuadStripToList(centers2, centers, i);
    QuadStripToList(widthType2, widthType, i);
    SetColor(baseColor, clr1, i);
    SetColor(outlineColor, clr2, i);
  }

  GLState state(gpu::SOLID_LINE_PROGRAM, GLState::GeometryLayer);
  AttributeProvider provider(6, 6*(size-1));

  {
    BindingInfo pos_dir(1);
    BindingDecl & decl = pos_dir.GetBindingDecl(0);
    decl.m_attributeName = "position";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;
    provider.InitStream(0, pos_dir, MakeStackRefPointer((void*)&vertex2[0]));
  }
  {
    BindingInfo deltas(1);
    BindingDecl & decl = deltas.GetBindingDecl(0);
    decl.m_attributeName = "deltas";
    decl.m_componentCount = 3;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;

    provider.InitStream(1, deltas, MakeStackRefPointer((void*)&dxVals2[0]));
  }
  {
    BindingInfo width_type(1);
    BindingDecl & decl = width_type.GetBindingDecl(0);
    decl.m_attributeName = "width_type";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;

    provider.InitStream(2, width_type, MakeStackRefPointer((void*)&widthType2[0]));
  }
  {
    BindingInfo centres(1);
    BindingDecl & decl = centres.GetBindingDecl(0);
    decl.m_attributeName = "centres";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;

    provider.InitStream(3, centres, MakeStackRefPointer((void*)&centers2[0]));
  }

  {
    BindingInfo clr1(1);
    BindingDecl & decl = clr1.GetBindingDecl(0);
    decl.m_attributeName = "color1";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;

    provider.InitStream(4, clr1, MakeStackRefPointer((void*)&baseColor[0]));
  }

  {
    BindingInfo clr2(1);
    BindingDecl & decl = clr2.GetBindingDecl(0);
    decl.m_attributeName = "color2";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;

    provider.InitStream(5, clr2, MakeStackRefPointer((void*)&outlineColor[0]));
  }

  batcher->InsertTriangleList(state, MakeStackRefPointer(&provider));
}

} // namespace df