bool FluidSimulationSaveState::loadState(std::string filename) {
    _stateData = LoadStateData();

    std::ifstream state(filename.c_str(), std::ios::in | std::ios::binary);

    int numMarkerParticles, numSolidCellIndices;

    bool success = _readInt(&_stateData.i, &state) &&
                   _readInt(&_stateData.j, &state) &&
                   _readInt(&_stateData.k, &state) &&
                   _readDouble(&_stateData.dx, &state) &&
                   _readInt(&numMarkerParticles, &state) &&
                   _readInt(&_stateData.currentFrame, &state) &&
                   _readInt(&numSolidCellIndices, &state);

    if (!success) {
        return false;
    }

    success = _readMarkerParticlePositions(_stateData.markerParticlePositions, 
                                           numMarkerParticles, &state);
    if (!success) {
        return false;
    }

    success = _readMarkerParticleVelocities(_stateData.markerParticleVelocities, 
                                            numMarkerParticles, &state);
    if (!success) {
        return false;
    }

    success = _readSolidCellIndices(_stateData.solidCellIndices, numSolidCellIndices, &state);
    if (!success) {
        return false;
    }

    _isLoadStateInitialized = true;
    return true;
}
Ejemplo n.º 2
0
static XmImageInfo*
readFLG(ImageBuffer *ib)
{
	XmImageInfo *all_frames, *frame, *current;
	int frameCount, i;
	Byte c, *data;
	Dimension *color;
	Cardinal nframes, loop_count;

	/* bogus frame leader */
	all_frames = (XmImageInfo*)malloc(sizeof(XmImageInfo));
	all_frames->frame = NULL;
	current = all_frames;

	/* skip magic */
	ib->next += 6;
	ib->curr_pos += 6;

	/* FLG revision */
	_readByte(c);

	/* compress byte */
	_readByte(c);

	/* original image type */
	_readByte(c);

	/* no of frames in this file */
	_readCardinal(nframes);

	/* no of times to repeat animation */
	_readCardinal(loop_count);

	/* read all frames */
	frameCount = 0;

	do{
		frame = (XmImageInfo*)calloc(1, sizeof(XmImageInfo));

		/* default options */
		frame->options = XmIMAGE_DEFERRED_FREE | XmIMAGE_RGB_SINGLE |
							XmIMAGE_ALLOW_SCALE;

		frame->type = IMAGE_FLG;

		/* frames remaining */
		frame->nframes = nframes - frameCount;
		frame->loop_count = loop_count;

		_readDimension(frame->width);
		_readDimension(frame->height);
		frame->swidth  = frame->width;
		frame->sheight = frame->height;

		/* logical screen offsets (these can be negative) */
		_readInt(frame->x);
		_readInt(frame->y);

		/* frame timeout */
		_readCardinal(frame->timeout);

		/* frame disposal method */
		_readByte(frame->dispose);

		/* frame depth (original bits per pixel) */
		_readByte(frame->depth);

		/* transparent pixel index */
		_readInt(frame->bg);

		/* transparency type */
		_readByte(frame->transparency);

		/* colorspace */
		_readByte(frame->colorspace);

		/* size of colormap */
		_readCardinal(frame->ncolors);

		if(frame->ncolors)
		{
			/* add one, it's zero based */
			frame->ncolors++;
			frame->scolors = frame->ncolors;

			/* alloc colormap */
			frame->reds = (Dimension*)calloc(3*frame->ncolors,
							sizeof(Dimension));

			/* read it */
			color = frame->reds;

			/* red color components */
			for(i = 0; i < frame->ncolors; i++, color++)
				_readDimension(*color);

			/* green color components */
			frame->greens = color;
			for(i = 0; i < frame->ncolors; i++, color++)
				_readDimension(*color);

			/* blue color components */
			frame->blues = color;
			for(i = 0; i < frame->ncolors; i++, color++)
				_readDimension(*color);
		}

		/* frame data */
		frame->data = (Byte*)malloc(frame->width * frame->height);
		data = frame->data;
		for(i = 0; i < frame->width * frame->height; i++)
			_readByte(*data++);

		/* clipmask data (if we have got one) */
		if(frame->bg != -1 && frame->transparency == XmIMAGE_TRANSPARENCY_BG)
		{
			int wd;
			Byte *clip;

			wd = frame->width;
			/* pad so it will be a multiple of 8 */
			while((wd % 8))
				wd++;

			/* this many bytes on a row */
			wd /= 8;
			/* this many bytes in the clipmask */
			wd *= frame->height;

			frame->clip = (Byte*)malloc(wd);
			clip = frame->clip;
			for(i = 0; i < wd; i++)
				_readByte(*clip++);

			/* we have a clipmask */
			frame->options |= XmIMAGE_CLIPMASK;
		}

		/* alpha channel (if any) */
		if(frame->transparency == XmIMAGE_TRANSPARENCY_ALPHA)
		{
			Cardinal gamma;
			Byte *alpha;

			/* frame gamma */
			_readCardinal(gamma);
			frame->fg_gamma = (float)gamma/100000.0;

			/* write out alpha channel */
			frame->alpha = (Byte*)malloc(frame->width * frame->height);
			alpha = frame->alpha;
			for(i = 0; i < frame->width * frame->height; i++)
				_readByte(*alpha++);
		}
		/* terminator */
		_readByte(c);

		if(c != 0)
			fprintf(stderr, "readFLG: missing separator bit on frame %i!\n",
				frameCount);

		current->frame = frame;
		current = current->frame;

		frameCount++;	/* done with this frame */
	}while(frameCount < nframes);

	current = all_frames->frame;
	free(all_frames);
	return(current);
}