forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_sum.cpp
209 lines (189 loc) · 4.92 KB
/
opennurbs_sum.cpp
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
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
ON_Sum::ON_Sum()
{
Begin(0.0);
}
int ON_Sum::SummandCount() const
{
return m_pos_count + m_neg_count + m_zero_count;
}
void ON_Sum::Begin( double starting_value )
{
m_sum_err = 0.0;
m_pos_sum = 0.0;
m_neg_sum = 0.0;
m_pos_sum1_count = 0;
m_pos_sum2_count = 0;
m_pos_sum3_count = 0;
m_neg_sum1_count = 0;
m_neg_sum2_count = 0;
m_neg_sum3_count = 0;
m_pos_count = 0;
m_neg_count = 0;
m_zero_count = 0;
if ( starting_value > 0.0 )
{
m_pos_sum = starting_value;
}
else if ( starting_value < 0.0 )
{
m_neg_sum = starting_value;
}
}
double ON_Sum::SortAndSum( int count, double* a )
{
// note that the arrays passed to ON_Sum::SortAndSum() are all
// homogeneous in sign
double s = 0.0;
if ( count > 0 )
{
if ( count >= 2 )
{
ON_SortDoubleArray( ON::sort_algorithm::quick_sort, a, count );
//double a0 = fabs(a[0]);
//double a1 = fabs(a[count-1]);
m_sum_err += ON_EPSILON*( fabs(a[count-1]) + count*fabs(a[0]) );
}
if ( a[count] < 0.0 )
{
a += count-1;
while (count--)
s += *a--;
}
else
{
while (count--)
s += *a++;
}
}
return s;
}
void ON_Sum::Plus( double x, double dx )
{
Plus(x);
if ( ON_IsValid(dx) )
m_sum_err += fabs(dx);
}
void ON_Sum::Plus( double x )
{
if (x > 0.0)
{
m_pos_count++;
m_pos_sum1[m_pos_sum1_count++] = x;
if ( m_pos_sum1_count == sum1_max_count )
{
m_pos_sum2[m_pos_sum2_count++] = SortAndSum( m_pos_sum1_count, m_pos_sum1 );
m_pos_sum1_count = 0;
if ( m_pos_sum2_count == sum2_max_count )
{
m_pos_sum3[m_pos_sum3_count++] = SortAndSum( m_pos_sum2_count, m_pos_sum2 );
m_pos_sum2_count = 0;
if ( m_pos_sum3_count == sum3_max_count )
{
x = SortAndSum( m_pos_sum3_count, m_pos_sum3 );
m_sum_err += ON_EPSILON*( fabs(x) + fabs(m_pos_sum) );
m_pos_sum += x;
m_pos_sum3_count = 0;
}
}
}
}
else if ( x < 0.0 )
{
m_neg_count++;
m_neg_sum1[m_neg_sum1_count++] = x;
if ( m_neg_sum1_count == sum1_max_count )
{
m_neg_sum2[m_neg_sum2_count++] = SortAndSum( m_neg_sum1_count, m_neg_sum1 );
m_neg_sum1_count = 0;
if ( m_neg_sum2_count == sum2_max_count )
{
m_neg_sum3[m_neg_sum3_count++] = SortAndSum( m_neg_sum2_count, m_neg_sum2 );
m_neg_sum2_count = 0;
if ( m_neg_sum3_count == sum3_max_count )
{
x = SortAndSum( m_neg_sum3_count, m_neg_sum3 );
m_sum_err += ON_EPSILON*( fabs(x) + fabs(m_neg_sum) );
m_neg_sum += x;
m_neg_sum3_count = 0;
}
}
}
}
else
m_zero_count++;
}
void ON_Sum::operator=(double x)
{
Begin(x);
}
void ON_Sum::operator+=(double x)
{
Plus(x);
}
void ON_Sum::operator-=(double x)
{
Plus(-x);
}
double ON_Sum::Total( double* error_estimate )
{
double x;
if ( m_pos_sum1_count > 0 )
{
m_pos_sum2[m_pos_sum2_count++] = SortAndSum( m_pos_sum1_count, m_pos_sum1 );
m_pos_sum1_count = 0;
}
if ( m_pos_sum2_count > 0 )
{
m_pos_sum3[m_pos_sum3_count++] = SortAndSum( m_pos_sum2_count, m_pos_sum2 );
m_pos_sum2_count = 0;
}
if ( m_pos_sum3_count > 0 )
{
x = SortAndSum( m_pos_sum3_count, m_pos_sum3 );
m_sum_err += ON_EPSILON*( fabs(x) + fabs(m_pos_sum) );
m_pos_sum += x;
m_pos_sum3_count = 0;
}
if ( m_neg_sum1_count > 0 )
{
m_neg_sum2[m_neg_sum2_count++] = SortAndSum( m_neg_sum1_count, m_neg_sum1 );
m_neg_sum1_count = 0;
}
if ( m_neg_sum2_count > 0 )
{
m_neg_sum3[m_neg_sum3_count++] = SortAndSum( m_neg_sum2_count, m_neg_sum2 );
m_neg_sum2_count = 0;
}
if ( m_neg_sum3_count > 0 )
{
x = SortAndSum( m_neg_sum3_count, m_neg_sum3 );
m_sum_err += ON_EPSILON*( fabs(x) + fabs(m_neg_sum) );
m_neg_sum += x;
m_neg_sum3_count = 0;
}
if ( error_estimate )
{
*error_estimate = m_sum_err + ON_EPSILON*(fabs(m_pos_sum) + fabs(m_neg_sum));
}
return m_pos_sum + m_neg_sum;
}