Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/lib/common/world.cxx
ViewVC logotype

View of /branches/vis15/src/lib/common/world.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4450 - (download) (as text) (annotate)
Mon Aug 22 18:33:57 2016 UTC (2 years, 10 months ago) by jhr
File size: 4242 byte(s)
  working on merge: bug fix for loading dynamic sequences
/*! \file world.cxx
 *
 * \author John Reppy
 */

/*
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 */

#include <cstdlib>
#include "diderot/base.hxx"
#include "diderot/world.hxx"

#ifdef DIDEROT_TARGET_PARALLEL
#include "diderot/parallel.hxx"
#endif

namespace diderot {

    world_base::world_base (std::string const &name, bool isArr, int nAxes)
	: _name(name), _errors(biffMsgNew("DIDEROT")), _stage(POST_NEW),
	  _verbose(false), _is_array(isArr), _naxes(nAxes),
	  _base(new uint32_t[nAxes]), _size(new uint32_t[nAxes]),
	  _nstrands(0), _output(nullptr), _run_time(0)
    {
#ifdef DIDEROT_TARGET_PARALLEL
	this->_sched = new diderot::scheduler();
	this->_sched->get_cpu_info (this);
	this->_sched->_numWorkers = this->_sched->_numHWCores;
#endif
    }

    world_base::~world_base ()
    {
        biffMsgNix (this->_errors);
        delete[] this->_base;
        delete[] this->_size;
        if (this->_output != nullptr) {
            delete this->_output;
        }
#ifdef DIDEROT_TARGET_PARALLEL
        delete this->_sched;
#endif
    }

    //! Check that a nrrd has the expected structure for loading into a dynamic sequence
    //! \param nin the nrrd to check
    //! \param nDims the number of dimensions in the sequence elements; for a sequence of
    //!              scalars nDims will be 0
    //! \param dims the array of sequence element dimensions
    //! \return the number of values per element, or zero on error
    uint32_t world_base::check_nrrd (const Nrrd *nin, uint32_t nDims, const uint32_t *dims)
    {
      // compute the expected number of values per sequence element
        uint32_t nValuesPerElem = 1;
        for (uint32_t i = 0;  i < nDims;  i++) {
            nValuesPerElem *= dims[i];
        }

      // check the structure of the nrrd file
	if ((nin->spaceDim != 0) && (nin->spaceDim != 1)) {
            this->error ("unexpected number of axes in nrrd; found %d\n",
                nin->spaceDim);
            return 0;
        }
        if (nin->dim-1 != nDims) {
            this->error ("unexpected nrrd dimension; expected %d, found %d\n", nDims, nin->dim-1);
            return 0;
        }
        for (unsigned int i = 0;  i < nDims;  i++) {
            if (dims[i] != nin->axis[i].size) {
                this->error ("nrrd axis %d does not match expected dimension: %d != %d\n",
                    i+1, dims[i], nin->axis[i].size);
                return 0;
            }
        }
        if (__details::nrrd_type_info[nin->type].sizeb == 0) {
            this->error ("bogus element type %d in nrrd\n", nin->type);
            return 0;
        }

        return nValuesPerElem;
    }

    Nrrd *world_base::load_nrrd_file (std::string const &filename, bool normalize)
    {
      /* create a nrrd; at this point it is just an empty container */
        Nrrd *nin = nrrdNew();

      /* read in the nrrd from the file */
        if (nrrdLoad(nin, filename.c_str(), nullptr) != 0) {
            char *msg = biffGetDone(NRRD);
            biffMsgAdd (this->_errors, msg);
            std::free (msg);
            return nullptr;
        }

#ifdef HAVE_NRRDMETADATANORMALIZE
	int lostMeasureFrame = 0;
	int sts = nrrdMetaDataNormalize (
		nin, nin, nrrdMetaDataCanonicalVersionAlpha,
		AIR_FALSE, AIR_FALSE, AIR_FALSE, 1.0, &lostMeasureFrame);
	if (sts != 0) {
	  // propagate the error message from nrrdMetaDataNormalize
            char *msg = biffGetDone(NRRD);
            biffMsgAdd (this->_errors, msg);
	    nrrdNuke (nin);
	    return nullptr;
	}
	else if (lostMeasureFrame != 0) {
// FIXME: we should figure out a mechanism to pass warnings back up the call chain.
	    std::cerr << "WARNING: input data measurement frame lost" << std::endl;
	}
#endif

        return nin;

    }

    void world_base::error (const char *fmt, ...)
    {
        char buf[1024];
        va_list ap;

    // NOTE: if there was a biffMsg function that worked with va_lists, then we
    // could avoid the buffer
        va_start (ap, fmt);
        vsnprintf (buf, sizeof(buf), fmt, ap);
        va_end (ap);

        biffMsgAdd (this->_errors, buf);
    }

} // namespace Diderot

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0