File: SLineCurve.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (192 lines) | stat: -rw-r--r-- 4,286 bytes parent folder | download
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
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "SLineCurve.h"
#include "stoolkit/SExcept.h"
#include "stoolkit/SUtil.h"

/**
 * A stroke is a path, a collection of SLocation points.
 * This object provides extra functionality:
 * it caches the length of the path as well as the length
 * distance between elements. Also it can calculate the
 * closest distance between two points. 
 * Gaspar Sinai
 */

/**
 * Create an empty stroke.
 */
SLineCurve::SLineCurve (void)
{
}

/**
 * Shallow copy this stroke.
 */
SLineCurve::SLineCurve (const SLineCurve& d)
{
  /* no need to check if we are 'd' */
  vectors = d.vectors;
  lengths = d.lengths;
}
/**
 */
SLineCurve
SLineCurve::operator=(const SLineCurve& d)
{
  if (&d == this) return *this;
  vectors = d.vectors;
  lengths = d.lengths;
  return *this;
}

SLineCurve::~SLineCurve ()
{
  /* nothing to do */
}

/**
 * Obligatory SObject stuff.
 */
SObject*
SLineCurve::clone() const
{
  SLineCurve* s = new SLineCurve (*this);
  CHECK_NEW (s);
  return s;
}

/**
 * @return the full length of the path - open path.
 */
unsigned int
SLineCurve::length () const
{
  return length (0, lengths.size()-1);
}

/**
 * Get the length of this segment.
 * @param segment is greater than zero and less than size
 * at least two elements to calculate this.
 */
unsigned int
SLineCurve::length (unsigned int segment) const
{
  return length (segment-1, segment);
}
/**
 * Get the length from and not including to.
 * @param from start point
 * @param to end point.
 * Points can not be reversed.
 */
unsigned int
SLineCurve::length (unsigned int from, unsigned int to) const
{
  return lengths[to] - lengths[from];
}

/**
 * Calculate the distance between the first and the last point 
 */
unsigned int
SLineCurve::distance () const
{
  if (vectors.size()==0) return 0;
  return distance (0, vectors.size()-1);
}
/**
 * Calculate the shortest distance between two points.
 * This is ALWAYS less or equal to length()
 * @param p0 start point
 * @param p1 end point
 * Points can not be reversed.
 */
unsigned int
SLineCurve::distance (unsigned int p0, unsigned int p1) const
{
  const SLocation diff = vectors[p1] - vectors[p0];
  return diff.distance();
}
const SLocation&
SLineCurve::operator[] (unsigned int index) const
{
  return vectors[index];
}

/**
 * Get a vector pointing from -> to
 * @param p0 start point
 * @param p1 end point
 * Points can not be reversed.
 */
SLocation
SLineCurve::getVector (unsigned int p0, unsigned int p1) const
{
  return SLocation (vectors[p1] - vectors[p0]);
}

unsigned int
SLineCurve::size() const
{
   return vectors.size();
}

void
SLineCurve::clear()
{
  vectors.clear();
  lengths.clear();
}

/**
 * Append a new location to this line of points.
 * check consistency of lengths too, remember
 * when a vector's length is calculated it is for sure
 * less than the real length, because of the rounding
 * in ss_sqrtlong routine used by SLocation.
 */
void 
SLineCurve::append (const SLocation& l)
{
  vectors.append (l);
  /* is this the only one ? */
  if (vectors.size()==1)
  {
     lengths.append (0);
     return;
  }
  unsigned int fulllengthnow = length ();
  unsigned int diagonal = distance ();
  /* vectors are already updated */
  unsigned int len = distance (vectors.size()-2, vectors.size()-1);
  /* sanity - length can not decrease */

  if (fulllengthnow + len < diagonal)
  {
   // Notice len can only be positive or zero so 
   //   we for sure append a positive number 
    lengths.append (diagonal);
    return;
  }
  lengths.append (fulllengthnow + len);
}