source: trunk/src/objects/RpHistogram.cc @ 6303

Last change on this file since 6303 was 5673, checked in by ldelgass, 9 years ago

Fix line endings, set eol-style to native on all C/C++ sources.

  • Property svn:eol-style set to native
File size: 10.7 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture 2.0 Histogram Object Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include "RpHistogram.h"
15#include "RpArray1DUniform.h"
16
17using namespace Rappture;
18
19const char Histogram::x[] = "xaxis";
20const char Histogram::y[] = "yaxis";
21
22Histogram::Histogram()
23    : Curve(),
24      _markerList(NULL)
25{}
26
27Histogram::Histogram(double *h, size_t npts)
28    : Curve(),
29      _markerList(NULL)
30{
31    Array1D *y = axis(Histogram::y,"","","","",h,npts);
32    Array1DUniform bins(y->min(),y->max(),y->nmemb());
33    axis(Histogram::x,"","","","",bins.data(),bins.nmemb());
34}
35
36Histogram::Histogram(double *h, size_t npts, double *bins, size_t nbins)
37    : Curve(),
38      _markerList(NULL)
39{
40    axis(Histogram::x,"","","","",bins,nbins);
41    axis(Histogram::y,"","","","",h,npts);
42}
43
44Histogram::Histogram(double *h, size_t npts, double min, double max,
45                     size_t nbins)
46    : Curve(),
47      _markerList(NULL)
48{
49    Array1DUniform bins(min,max,nbins);
50    axis(Histogram::x,"","","","",bins.data(),bins.nmemb());
51    axis(Histogram::y,"","","","",h,npts);
52}
53
54Histogram::Histogram(double *h, size_t npts, double min, double max,
55                     double step)
56    : Curve(),
57      _markerList(NULL)
58{
59    Array1DUniform bins(min,max,step);
60    axis(Histogram::x,"","","","",bins.data(),bins.nmemb());
61    axis(Histogram::y,"","","","",h,npts);
62}
63
64// copy constructor
65Histogram::Histogram(const Histogram& o)
66    : Curve(o),
67      _markerList(NULL)
68{
69    group(o.group());
70
71    if (o._markerList != NULL) {
72        Rp_ChainLink *l = Rp_ChainFirstLink(o._markerList);
73        while (l != NULL) {
74            AxisMarker *a = (AxisMarker *) Rp_ChainGetValue(l);
75            AxisMarker *newAM = new AxisMarker(*a);
76            Rp_ChainAppend(_markerList,(void *)newAM);
77            l = Rp_ChainNextLink(l);
78        }
79    }
80
81}
82
83// default destructor
84Histogram::~Histogram ()
85{
86    // clean up dynamic memory
87    // unallocate the _axisList?
88
89}
90
91/**********************************************************************/
92// METHOD: xaxis()
93/// Define the xaxis properties
94/**
95 * Define the xaxis properties.
96 */
97
98Histogram&
99Histogram::xaxis(const char *label, const char *desc, const char *units)
100{
101    Array1D *a = getAxis(Histogram::x);
102
103    if (a == NULL) {
104        // couldn't find xaxis array, create one
105        a = axis(Histogram::x,label,desc,units,"linear",NULL,0);
106    } else {
107        a->label(label);
108        a->desc(desc);
109        a->units(units);
110    }
111
112    return *this;
113}
114
115/**********************************************************************/
116// METHOD: xaxis()
117/// Define the xaxis properties
118/**
119 * Define the xaxis properties.
120 */
121
122Histogram&
123Histogram::xaxis(const char *label, const char *desc,
124                 const char *units, const double *bins, size_t nBins)
125{
126    Array1D *a = getAxis(Histogram::x);
127
128    if (a == NULL) {
129        // couldn't find xaxis array, create one
130        a = axis(Histogram::x,label,desc,units,"linear",bins,nBins);
131    } else {
132        a->label(label);
133        a->desc(desc);
134        a->units(units);
135        a->clear();
136        a->append(bins,nBins);
137    }
138
139    return *this;
140}
141
142/**********************************************************************/
143// METHOD: xaxis()
144/// Define the xaxis properties
145/**
146 * Define the xaxis properties.
147 */
148
149Histogram&
150Histogram::xaxis(const char *label, const char *desc,
151                 const char *units, double min, double max,
152                 size_t nBins)
153{
154    Array1D *a = getAxis(Histogram::x);
155    Array1DUniform tmp(min,max,nBins);
156
157    if (a == NULL) {
158        // couldn't find xaxis array, create one
159        a = axis(Histogram::x,label,desc,units,"linear",
160                 tmp.data(),tmp.nmemb());
161    } else {
162        a->label(label);
163        a->desc(desc);
164        a->units(units);
165        a->clear();
166        a->append(tmp.data(),tmp.nmemb());
167    }
168
169    return *this;
170}
171
172/**********************************************************************/
173// METHOD: xaxis()
174/// Define the xaxis properties
175/**
176 * Define the xaxis properties.
177 */
178
179Histogram&
180Histogram::xaxis(const char *label, const char *desc,
181                 const char *units, double min, double max,
182                 double step)
183{
184    Array1D *a = getAxis(Histogram::x);
185    Array1DUniform tmp(min,max,step);
186
187    if (a == NULL) {
188        // couldn't find xaxis array, create one
189        a = axis(Histogram::x,label,desc,units,"linear",
190                 tmp.data(),tmp.nmemb());
191    } else {
192        a->label(label);
193        a->desc(desc);
194        a->units(units);
195        a->clear();
196        a->append(tmp.data(),tmp.nmemb());
197    }
198
199    return *this;
200}
201
202/**********************************************************************/
203// METHOD: yaxis()
204/// Define the yaxis properties
205/**
206 * Define the yaxis properties.
207 */
208
209Histogram&
210Histogram::yaxis(const char *label, const char *desc, const char *units)
211{
212    Array1D *a = getAxis(Histogram::y);
213
214    if (a == NULL) {
215        // couldn't find yaxis array, create one
216        a = axis(Histogram::y,label,desc,units,"linear",NULL,0);
217    } else {
218        a->label(label);
219        a->desc(desc);
220        a->units(units);
221    }
222
223    return *this;
224}
225
226/**********************************************************************/
227// METHOD: yaxis()
228/// Define the yaxis properties
229/**
230 * Define the yaxis properties.
231 */
232
233Histogram&
234Histogram::yaxis(const char *label, const char *desc,
235                 const char *units, const double *vals,
236                 size_t nPts)
237{
238    Array1D *a = getAxis(Histogram::y);
239
240    if (a == NULL) {
241        // couldn't find xaxis array, create one
242        a = axis(Histogram::y,label,desc,units,"linear",vals,nPts);
243    } else {
244        a->label(label);
245        a->desc(desc);
246        a->units(units);
247        a->clear();
248        a->append(vals,nPts);
249    }
250
251    return *this;
252}
253
254/**********************************************************************/
255// METHOD: binWidths()
256/// Define the bin widths
257/**
258 * Define the bin widths
259 */
260
261Histogram&
262Histogram::binWidths(const size_t *widths, size_t nbins)
263{
264    // FIXME: we can probably find a better way to do this evenatually
265    // Dont know how to get an array of size_t's in the axis list yet
266    // maybe we store it separately, or Templatize Array1D
267
268    SimpleDoubleBuffer b;
269    if (widths == NULL) {
270        return *this;
271    }
272
273    b.set(nbins);
274    for (size_t i=0; i < nbins; i++) {
275        double val = static_cast<double>(*(widths+i));
276        b.append(&val,1);
277    }
278    axis("binwidths","","","","",b.bytes(),b.nmemb());
279
280    return *this;
281}
282
283/**********************************************************************/
284// METHOD: marker()
285/// Define a new marker
286/**
287 * Define a new marker
288 */
289
290Histogram&
291Histogram::marker(const char *axisName, double at, const char *label,
292                  const char *style)
293{
294    AxisMarker *m = new AxisMarker(axisName,label,style,at);
295
296    if (_markerList == NULL) {
297        _markerList = Rp_ChainCreate();
298        if (_markerList == NULL) {
299            delete m;
300            return *this;
301        }
302    }
303
304    Rp_ChainAppend(_markerList,(void*)m);
305
306    return *this;
307}
308
309/**********************************************************************/
310// METHOD: xml()
311/// Return the xml of the object
312/**
313 * Return the xml of the object
314 */
315
316const char *
317Histogram::xml(size_t indent, size_t tabstop)
318{
319    size_t l1width = indent + tabstop;
320    size_t l2width = indent + (2*tabstop);
321    const char *sp = "";
322
323    Path p(path());
324
325    Array1D *tmpAxis = NULL;
326    size_t nmemb = 0;
327
328    const double *dataArr[dims()];
329
330    _tmpBuf.clear();
331
332    _tmpBuf.appendf(
333"%8$*5$s<histogram id=\"%1$s\">\n\
334%8$*6$s<about>\n\
335%8$*7$s<group>%2$s</group>\n\
336%8$*7$s<label>%3$s</label>\n\
337%8$*7$s<description>%4$s</description>\n\
338%8$*6$s</about>\n",
339        p.id(),group(),label(),desc(),indent,l1width,l2width,sp);
340
341    for (size_t dim=0; dim < dims(); dim++) {
342        tmpAxis = getNthAxis(dim);
343        nmemb = tmpAxis->nmemb();
344        dataArr[dim] = tmpAxis->data();
345        if (strcmp("binwidths",tmpAxis->name()) == 0) {
346            continue;
347        }
348        _tmpBuf.appendf(
349"%8$*6$s<%1$s>\n\
350%8$*7$s<label>%2$s</label>\n\
351%8$*7$s<description>%3$s</description>\n\
352%8$*7$s<units>%4$s</units>\n\
353%8$*7$s<scale>%5$s</scale>\n",
354        tmpAxis->name(), tmpAxis->label(), tmpAxis->desc(),
355        tmpAxis->units(), tmpAxis->scale(),l1width,l2width,sp);
356
357        if (_markerList != NULL) {
358            Rp_ChainLink *l = Rp_ChainFirstLink(_markerList);
359            while (l != NULL) {
360                AxisMarker *m = (AxisMarker *) Rp_ChainGetValue(l);
361                if (strcmp(tmpAxis->name(),m->axisName()) == 0) {
362                    _tmpBuf.append(m->xml(indent+tabstop,tabstop));
363                }
364                l = Rp_ChainNextLink(l);
365            }
366        }
367        _tmpBuf.appendf("%3$*2$s</%1$s>\n",tmpAxis->name(),l1width,sp);
368    }
369
370    _tmpBuf.appendf("%3$*1$s<component>\n%3$*2$s<xhw>\n",l1width,l2width,sp);
371    for (size_t idx=0; idx < nmemb; idx++) {
372        for (size_t dim=0; dim < dims(); dim++) {
373            _tmpBuf.appendf("%10g",dataArr[dim][idx]);
374        }
375        _tmpBuf.append("\n",1);
376    }
377    _tmpBuf.appendf("%4$*3$s</xhw>\n%4$*2$s</component>\n%4$*1$s</curve>",
378        indent,l1width,l2width,sp);
379
380    return _tmpBuf.bytes();
381}
382
383/**********************************************************************/
384// METHOD: is()
385/// what kind of object is this
386/**
387 * return hex value telling what kind of object this is.
388 */
389
390const int
391Histogram::is() const
392{
393    // return "hist" in hex
394    return 0x68697374;
395}
396
397/**********************************************************************/
398// FUNCTION: axisMarkerCpyFxn()
399/// Copy an axisMarker object
400/**
401 * Copy an axisMarker object
402 */
403
404/*
405int
406axisMarkerCpyFxn(void **to, void *from)
407{
408    int retVal = 0;
409    size_t len = 0;
410    char *tmp = NULL;
411    Histogram::axisMarker *totmp = NULL;
412
413    if (from == NULL) {
414        return -1;
415    }
416
417    totmp = new Historgram::axisMarker();
418
419    len = strlen(from->axisName);
420    tmp = new char[len+1];
421    strncpy(tmp,from->axisName,len+1);
422    totmp->axisName = tmp;
423
424    len = strlen(from->label);
425    tmp = new char[len+1];
426    strncpy(tmp,from->label,len+1);
427    totmp->label = tmp;
428
429    len = strlen(from->style);
430    tmp = new char[len+1];
431    strncpy(tmp,from->style,len+1);
432    totmp->style = tmp;
433
434    totmp->at = at;
435
436    *to = totmp;
437
438    return retVal;
439}
440*/
441
442// -------------------------------------------------------------------- //
443
Note: See TracBrowser for help on using the repository browser.