forked from eu07/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curve.cpp
146 lines (125 loc) · 3.05 KB
/
Curve.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
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "system.hpp"
#include "classes.hpp"
#pragma hdrstop
#include "Curve.h"
TCurve::TCurve()
{
Values = NULL;
iNumValues = 0;
iNumCols = 0;
}
TCurve::~TCurve()
{
for (int i = 0; i < iNumValues; i++)
SafeDelete(Values[i]);
SafeDelete(Values);
}
bool TCurve::Init(int n, int c)
{
for (int i = 0; i < iNumValues; i++)
SafeDelete(Values[i]);
SafeDelete(Values);
iNumValues = n;
iNumCols = c;
Values = new float *[iNumValues];
for (int i = 0; i < iNumValues; i++)
Values[i] = new float[iNumCols];
for (int i = 0; i < iNumValues; i++)
for (int j = 0; j < iNumCols; j++)
Values[i][j] = 0;
}
float TCurve::GetValue(int c, float p)
{
int a = floor(p);
int b = ceil(p);
if (a < 0)
return Values[0][c];
if (b >= iNumValues)
return Values[iNumValues - 1][c];
p -= floor(p);
return Values[a][c] * (1.0f - p) + Values[b][c] * (p);
}
bool TCurve::SetValue(int c, float p, float v)
{
int a = floor(p);
int b = ceil(p);
if (a < 0)
return false;
if (b >= iNumValues)
return false;
p -= floor(p);
if (p < 0.5)
Values[a][c] = v;
else
Values[b][c] = v;
return true;
}
bool TCurve::Load(TQueryParserComp *Parser)
{
DecimalSeparator = '.';
AnsiString Token;
int n = Parser->GetNextSymbol().ToInt();
int c = Parser->GetNextSymbol().ToInt();
Init(n, c);
n = 0;
int i;
while (!Parser->EOF && n < iNumValues)
{
for (i = 0; i < iNumCols; i++)
Values[n][i] = Parser->GetNextSymbol().ToDouble();
n++;
}
DecimalSeparator = ',';
}
bool TCurve::LoadFromFile(AnsiString asName)
{
DecimalSeparator = '.';
TFileStream *fs;
fs = new TFileStream(asName, fmOpenRead | fmShareCompat);
AnsiString str = "xxx";
int size = fs->Size;
str.SetLength(size);
fs->Read(str.c_str(), size);
str += "";
delete fs;
TQueryParserComp *Parser;
Parser = new TQueryParserComp(NULL);
Parser->TextToParse = str;
Parser->First();
Load(Parser);
delete Parser;
DecimalSeparator = ',';
}
#include <stdio.h>
bool TCurve::SaveToFile(AnsiString asName)
{
DecimalSeparator = '.';
FILE *stream = NULL;
stream = fopen(asName.c_str(), "w");
AnsiString str;
str = AnsiString(iNumValues);
fprintf(stream, str.c_str());
fprintf(stream, "\n");
for (int i = 0; i < iNumValues; i++)
{
str = "";
if (i % 10 == 0)
str += "\n";
for (int j = 0; j < iNumCols; j++)
str += FloatToStrF(Values[i][j], ffFixed, 6, 2) + AnsiString(" ");
str += AnsiString("\n");
fprintf(stream, str.c_str());
}
fclose(stream);
DecimalSeparator = ',';
}
//---------------------------------------------------------------------------
#pragma package(smart_init)