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 4030 - (download) (as text) (annotate)
Thu Jun 23 04:28:53 2016 UTC (3 years, 4 months ago) by jhr
File size: 2388 byte(s)
  Working on merge: dynamic sequence loading
/*! \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"

namespace diderot {

    //! 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
    //! \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) {
	    this->error ("unexpected number of axes in nrrd; expected 0, 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)
    {
      /* 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;
	}

	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