data_storage.cpp 9.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions

   Copyright (C) 2013, 2014, 2015 Inria

   This file is part of ALTA.

   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 "data.h"
#include "data_storage.h"
#include "vertical_segment.h"

#include <iostream>
#include <limits>
17
#include <cassert>
18

19 20 21 22
#ifdef __GLIBC__
# include <endian.h>
#endif

23
void vertical_segment::load_data_from_text(std::istream& input,
24
																					 const arguments& header,
25 26 27 28 29 30 31 32
																					 vertical_segment& result,
																					 const arguments& args)
{
	vec min, max ;
	vec ymin, ymax;

	result._nX = 0 ; result._nY = 0 ;

33 34 35 36 37 38 39 40 41
	if(! header.is_defined("DIM")) {
		std::cerr << "<<ERROR>> Undefined dimensions ! ";
		std::cerr << "Please add DIM [int] [int] into the file header." << std::endl;
		throw;
	}

	params::input  in_param  = params::parse_input(header.get_string("PARAM_IN", "UNKNOWN_INPUT"));
	params::output out_param = params::parse_output(header.get_string("PARAM_OUT", "UNKNOWN_OUTPUT"));
	result.setParametrizations(in_param, out_param);
42

43 44 45
	std::pair<int, int> dim = header.get_pair<int>("DIM");
	result.setDimX(dim.first);
	result.setDimY(dim.second);
46

47 48
	min = args.get_vec("min", result._nX, -std::numeric_limits<float>::max()) ;
	max = args.get_vec("max", result._nX,  std::numeric_limits<float>::max()) ;
49
#ifdef DEBUG
50
	std::cout << "<<DEBUG>> data will remove outside of " << min << " -> " << max << " x-interval" << std::endl;
51 52
#endif

53 54
	ymin = args.get_vec("ymin", result._nY, -std::numeric_limits<float>::max()) ;
	ymax = args.get_vec("ymax", result._nY,  std::numeric_limits<float>::max()) ;
55
#ifdef DEBUG
56
	std::cout << "<<DEBUG>> data will remove outside of " << ymin << " -> " << ymax << " y-interval" << std::endl;
57 58
#endif

59 60 61 62 63 64
	for(int k=0; k<result.dimX(); ++k)
	{
			result._min[k] =  std::numeric_limits<double>::max() ;
			result._max[k] = -std::numeric_limits<double>::max() ;
	}

65
	int vs_value = header.get_int("VS");
66 67 68 69 70 71 72 73 74 75

	// Now read the body.
	while(input.good())
	{
		std::string line ;
		std::getline(input, line) ;
		std::stringstream linestream(line) ;

		// Discard comments and empty lines.
		if(line.empty() || linestream.peek() == '#')
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
		{
			continue ;
		}
		else
		{
			// Read the data point x and y coordinates
			vec v = vec::Zero(result.dimX() + 3*result.dimY()) ;
			for(int i=0; i<result.dimX()+result.dimY(); ++i) 
			{
				linestream >> v[i] ;
			}

			// If data is not in the interval of fit
			bool is_in = true ;
			for(int i=0; i<result.dimX(); ++i)
			{
				if(v[i] < min[i] || v[i] > max[i])
				{
					is_in = false ;
				}
			}
			for(int i=0; i<result.dimY(); ++i)
			{
				if(v[result.dimX()+i] < ymin[i] || v[result.dimX()+i] > ymax[i])
				{
					is_in = false ;
				}
			}
			if(!is_in)
			{
				continue ;
			}

//			/*
			// Correction of the data by 1/cosine(theta_L)
			double factor = 1.0;
			if(args.is_defined("data-correct-cosine"))
			{
				double cart[6];
				params::convert(&v[0], result.input_parametrization(), params::CARTESIAN, cart);
				if(cart[5] > 0.0 && cart[2] > 0.0)
				{
					factor = 1.0/cart[5]*cart[2];
					for(int i=0; i<result.dimY(); ++i) 
					{
						v[i + result.dimX()] /= factor;
					}
				}
				else
				{
					continue;
				}
			}
			// End of correction
//			*/

			// Check if the data containt a vertical segment around the mean
			// value.
			for(int i=0; i<result.dimY(); ++i)
			{
				double min_dt = 0.0;
				double max_dt = 0.0;


140
				if(i == 0 && vs_value == 2)
141 142 143 144 145 146
				{
					linestream >> min_dt ;
					linestream >> max_dt ;
					min_dt = min_dt-v[result.dimX()+i];
					max_dt = max_dt-v[result.dimX()+i];
				}
147
				else if(i == 0 && vs_value == 1)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
				{
					double dt ;
					linestream >> dt ;
					min_dt = -dt;
					max_dt =  dt;
				}
				else
				{
					double dt = args.get_float("dt", 0.1f);
					min_dt = -dt;
					max_dt =  dt;
				}

				if(args.is_defined("dt-relative"))
				{
               v[result.dimX() +   result.dimY()+i] = v[result.dimX() + i] * (1.0 + min_dt) ;
					v[result.dimX() + 2*result.dimY()+i] = v[result.dimX() + i] * (1.0 + max_dt) ;
				}
				else if(args.is_defined("dt-max"))
				{
               v[result.dimX() +   result.dimY()+i] = v[result.dimX() + i] + std::max(v[result.dimX() + i] * min_dt, min_dt);
					v[result.dimX() + 2*result.dimY()+i] = v[result.dimX() + i] + std::max(v[result.dimX() + i] * max_dt, max_dt);
				}
				else
				{
					v[result.dimX() +   result.dimY()+i] = v[result.dimX() + i] + min_dt ;
					v[result.dimX() + 2*result.dimY()+i] = v[result.dimX() + i] + max_dt ;
				}

				// You can enforce the vertical segment to stay in the positive
				// region using the --data-positive command line argument. Note
				// that the data point is also clamped to zero if negative.
				if(args.is_defined("dt-positive"))
				{
					v[result.dimX() +          i] = std::max(v[result.dimX() +          i], 0.0);
					v[result.dimX() +   result.dimY()+i] = std::max(v[result.dimX() +   result.dimY()+i], 0.0);
					v[result.dimX() + 2*result.dimY()+i] = std::max(v[result.dimX() + 2*result.dimY()+i], 0.0);
				}

#ifdef DEBUG
                std::cout << "<<DEBUG>> vs = [" << v[result.dimX() +   result.dimY()+i] << ", " << v[result.dimX() + 2*result.dimY()+i] << "]" << std::endl;
#endif
			}

			result._data.push_back(v) ;

			// Update min and max
			for(int k=0; k<result.dimX(); ++k)
			{
				result._min[k] = std::min(result._min[k], v[k]) ;
				result._max[k] = std::max(result._max[k], v[k]) ;
			}
		}
	}

	if(args.is_defined("data-correct-cosine"))
		result.save("/tmp/data-corrected.dat");

	std::cout << "<<INFO>> loaded input stream" << std::endl ;
	std::cout << "<<INFO>> data inside " << result._min << " ... " << result._max << std::endl ;
	std::cout << "<<INFO>> loading data input of R^" << result.dimX() << " -> R^" << result.dimY() << std::endl ;
	std::cout << "<<INFO>> " << result._data.size() << " elements to fit" << std::endl ;
}

void save_data_as_text(std::ostream& out, const data &data)
{
		out << "#DIM " << data.dimX() << " " << data.dimY() << std::endl;
		out << "#PARAM_IN  " << params::get_name(data.input_parametrization())  << std::endl;
		out << "#PARAM_OUT " << params::get_name(data.output_parametrization()) << std::endl;
		for(int i=0; i < data.size(); ++i)
		{
				vec x = data.get(i);
				for(int j=0; j< data.dimX() + data.dimY(); ++j)
				{
						out << x[j] << "\t";
				}
				out << std::endl;
		}
}
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

void save_data_as_binary(std::ostream &out, const data& data)
{
		out << "#DIM " << data.dimX() << " " << data.dimY() << std::endl;
		out << "#PARAM_IN  " << params::get_name(data.input_parametrization())  << std::endl;
		out << "#PARAM_OUT " << params::get_name(data.output_parametrization()) << std::endl;
		out << "#FORMAT binary" << std::endl;
		out << "#VERSION 0" << std::endl;
		out << "#PRECISION ieee754-double" << std::endl;
		out << "#SAMPLE_COUNT " << data.size() << std::endl;

		// FIXME: Note: on non-glibc systems, both macros may be undefined, so
		// the conditional is equivalent to "#if 0 == 0", which is usually what
		// we want.
#if __BYTE_ORDER == __LITTLE_ENDIAN
		out << "#ENDIAN little" << std::endl;
#else
		out << "#ENDIAN big" << std::endl;
#endif

		out << "#BEGIN_STREAM" << std::endl;

		for(int i=0; i < data.size(); ++i)
		{
				vec sample = data.get(i);
				const double *numbers = sample.data();

				assert(sample.size() == data.dimX() + data.dimY());
				out.write((const char *)numbers, sample.size() * sizeof(*numbers));
		}

		out << std::endl << "#END_STREAM" << std::endl;
}
260

261
void load_data_from_binary(std::istream& in, const arguments& header, data& data)
262 263
{
		// FIXME: For now we make a number of assumptions.
264 265 266
		assert(header["FORMAT"] == "binary");
		assert(header.get_int("VERSION") == 0);
		assert(header["PRECISION"] == "ieee754-double");
267
#if __BYTE_ORDER == __LITTLE_ENDIAN
268
		assert(header["ENDIAN"] == "little");
269
#else
270
		assert(header["ENDIAN"] == "big");
271 272
#endif

273 274 275 276
		data.setParametrizations(params::parse_input(header["PARAM_IN"]),
										 params::parse_output(header["PARAM_OUT"]));

		std::pair<int, int> dim = header.get_pair<int>("DIM");
277 278 279 280 281
		data.setDimX(dim.first);
		data.setDimY(dim.second);

		in.exceptions(std::ios_base::failbit);

282
		int sample_count = header.get_int("SAMPLE_COUNT");
283

284 285 286 287 288 289 290 291 292
		// Initialize the mininum and maximum values.
		vec min(dim.first), max(dim.first);

		for(int k = 0; k < dim.first; k++)
		{
				min[k] =  std::numeric_limits<double>::max();
				max[k] = -std::numeric_limits<double>::max();
		}

293 294 295 296 297 298 299 300 301 302 303 304 305 306
		// TODO: Arrage to use mmap and make it zero-alloc and zero-copy.
		for (int i = 0; i < sample_count; i++)
		{
				vec row = vec::Zero(data.dimX() + data.dimY());
				std::streamsize expected = row.size() * sizeof(double);

				for (std::streamsize total = 0;
						 total < expected && !in.eof();
						 total += in.gcount())
				{
						in.read((char *)row.data() + total, expected);
				}

				data.set(row);
307 308 309 310 311 312 313

				// Update min and max.
				for(int k = 0; k < dim.first; k++)
				{
						min[k] = std::min(min[k], row[k]);
						max[k] = std::max(max[k], row[k]);
				}
314 315
		}

316 317 318
		// TODO: Factorize this in data::set.
		data.setMin(min);
		data.setMax(max);
319
}