]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGSmplhist.cxx
Working 'noshadow' animation
[simgear.git] / simgear / structure / SGSmplhist.cxx
1 // This may look like C code, but it is really -*- C++ -*-
2 /* 
3 Copyright (C) 1988 Free Software Foundation
4     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
5
6 This file is part of the GNU C++ Library.  This library is free
7 software; you can redistribute it and/or modify it under the terms of
8 the GNU Library General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.  This library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.  See the GNU Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <simgear_config.h>
21 #endif
22
23 #include <iostream>
24 #include <fstream>
25 #include "SGSmplhist.hxx"
26 #include <math.h>
27
28 #ifndef HUGE_VAL
29 #ifdef HUGE
30 #define HUGE_VAL HUGE
31 #else
32 #include <float.h>
33 #define HUGE_VAL DBL_MAX
34 #endif
35 #endif
36
37 const int SampleHistogramMinimum = -2;
38 const int SampleHistogramMaximum = -1;
39
40 SampleHistogram::SampleHistogram (double low, double high, double width)
41 {
42   if (high < low)
43     {
44       double t = high;
45       high = low;
46       low = t;
47     }
48
49   if (width == -1)
50     {
51       width = (high - low) / 10;
52     }
53
54   howManyBuckets = int ((high - low) / width) + 2;
55   bucketCount = new int[howManyBuckets];
56   bucketLimit = new double[howManyBuckets];
57   double lim = low;
58   for (int i = 0; i < howManyBuckets; i++)
59     {
60       bucketCount[i] = 0;
61       bucketLimit[i] = lim;
62       lim += width;
63     }
64   bucketLimit[howManyBuckets - 1] = HUGE_VAL;   /* from math.h */
65 }
66
67 SampleHistogram::~SampleHistogram ()
68 {
69   if (howManyBuckets > 0)
70     {
71       delete[]bucketCount;
72       delete[]bucketLimit;
73     }
74 }
75
76 void SampleHistogram::operator += (double value)
77 {
78   int i;
79   for (i = 0; i < howManyBuckets; i++)
80     {
81       if (value < bucketLimit[i])
82         break;
83     }
84   bucketCount[i]++;
85   this->SampleStatistic::operator += (value);
86 }
87
88 int SampleHistogram::similarSamples (double d)
89 {
90   int i;
91   for (i = 0; i < howManyBuckets; i++)
92     {
93       if (d < bucketLimit[i])
94         return (bucketCount[i]);
95     }
96   return (0);
97 }
98
99 void SampleHistogram::printBuckets (std::ostream & s)
100 {
101   for (int i = 0; i < howManyBuckets; i++)
102     {
103       if (bucketLimit[i] >= HUGE_VAL)
104         {
105           s << "< max : " << bucketCount[i] << "\n";
106         }
107       else
108         {
109           s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
110         }
111     }
112 }
113
114 void SampleHistogram::reset ()
115 {
116   this->SampleStatistic::reset ();
117   if (howManyBuckets > 0)
118     {
119       for (register int i = 0; i < howManyBuckets; i++)
120         {
121           bucketCount[i] = 0;
122         }
123     }
124 }