Beispiel #1
0
bool Player::setVolumeGroup(double volume)
{
  if (m_player)
  {
    bool ret = true;
    if (roundDouble(volume) == m_RCGroup.volume)
      return true;
    double r = (volume > 0 ? volume : 1.0);
    if (m_RCGroup.volumeFake > 0.0)
       r /= m_RCGroup.volumeFake;
    for (RCTable::iterator it = m_RCTable.begin(); it != m_RCTable.end(); ++it)
    {
      double fake = it->volumeFake * r;
      int v = roundDouble(fake < 1.0 ? 0.0 : fake < 100.0 ? fake : 100.0);
      SONOS::DBG(DBG_DEBUG, "%s: req=%3.3f ratio=%3.3f fake=%3.3f vol=%d\n", __FUNCTION__, volume, r, fake, v);
      if (m_player->SetVolume(it->uuid, v))
        it->volumeFake = fake;
      else
        ret = false;
    }
    if (ret)
      m_RCGroup.volume = roundDouble(m_RCGroup.volumeFake = volume);
    emit renderingChanged();
    return ret;
  }
  return false;
}
int writeMax(EmbPattern* pattern, const char* fileName)
{
	EmbStitchList* pointer;
	char header[] = {0x56,0x43,0x53,0x4D,0xFC,0x03,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
		0xF6,0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x05,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x33,0x37,0x38,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4D,0x61,0x64,0x65,0x69,0x72,0x61,0x20,
		0x52,0x61,0x79,0x6F,0x6E,0x20,0x34,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x01,0x38,0x09,0x31,0x33,0x30,0x2F,0x37,0x30,0x35,0x20,0x48,0xFA,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00};
	FILE *file = fopen(fileName, "wb");
    if(file == 0)
    {
        return 0;
    }
	binaryWriteBytes(file, header, 0xD5);
	pointer = pattern->stitchList;
    while(pointer)
    {
        maxEncode(file, roundDouble(pointer->stitch.xx * 10.0), roundDouble(pointer->stitch.yy * 10.0));
        pointer = pointer->next;
    }
	fclose(file);
    return 1;
}
Beispiel #3
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeExp(EmbPattern* pattern, const char* fileName)
{
#ifdef ARDUINO /* ARDUINO TODO: This is temporary. Remove when complete. */
return 0; /* ARDUINO TODO: This is temporary. Remove when complete. */
#else /* ARDUINO TODO: This is temporary. Remove when complete. */

    EmbFile* file = 0;
    EmbStitchList* stitches = 0;
    double dx = 0.0, dy = 0.0;
    double xx = 0.0, yy = 0.0;
    int flags = 0;
    unsigned char b[4];

    if(!pattern) { embLog_error("format-exp.c writeExp(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-exp.c writeExp(), fileName argument is null\n"); return 0; }

    if(!embStitchList_count(pattern->stitchList))
    {
        embLog_error("format-exp.c writeExp(), pattern contains no stitches\n");
        return 0;
    }

    /* Check for an END stitch and add one if it is not present */
    if(pattern->lastStitch->stitch.flags != END)
        embPattern_addStitchRel(pattern, 0, 0, END, 1);

    file = embFile_open(fileName, "wb");
    if(!file)
    {
        embLog_error("format-exp.c writeExp(), cannot open %s for writing\n", fileName);
        return 0;
    }

    /* write stitches */
    stitches = pattern->stitchList;
    while(stitches)
    {
        dx = stitches->stitch.xx * 10.0 - xx;
        dy = stitches->stitch.yy * 10.0 - yy;
        xx = stitches->stitch.xx * 10.0;
        yy = stitches->stitch.yy * 10.0;
        flags = stitches->stitch.flags;
        expEncode(b, (char)roundDouble(dx), (char)roundDouble(dy), flags);
        if((b[0] == 128) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4)))
        {
            embFile_printf(file, "%c%c%c%c", b[0], b[1], b[2], b[3]);
        }
        else
        {
            embFile_printf(file, "%c%c", b[0], b[1]);
        }
        stitches = stitches->next;
    }
    embFile_printf(file, "\x1a");
    embFile_close(file);
    return 1;
#endif /* ARDUINO TODO: This is temporary. Remove when complete. */
}
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeT01(EmbPattern* pattern, const char* fileName)
{
	EmbRect boundingRect;
	EmbFile* file = 0;
	int xx, yy, dx, dy, flags;
	int co = 1, st = 0;
	int ax, ay, mx, my;
	EmbStitchList* pointer = 0;
	
	if (!embStitchList_count(pattern->stitchList))
	{
		embLog_error("format-t01.c writeDst(), pattern contains no stitches\n");
		return 0;
	}

	/* Check for an END stitch and add one if it is not present */
	if (pattern->lastStitch->stitch.flags != END)
		embPattern_addStitchRel(pattern, 0, 0, END, 1);

	file = embFile_open(fileName, "wb");
	if (!file)
	{
		embLog_error("format-t01.c writet01(), cannot open %s for writing\n", fileName);
		return 0;
	}

	embPattern_correctForMaxStitchLength(pattern, 12.1, 12.1);

	xx = yy = 0;
	co = 1;
	co = embThreadList_count(pattern->threadList);
	st = 0;
	st = embStitchList_count(pattern->stitchList);
	flags = NORMAL;
	boundingRect = embPattern_calcBoundingBox(pattern);
	ax = ay = mx = my = 0;
	xx = yy = 0;
	pointer = pattern->stitchList;
	while (pointer)
	{
		/* convert from mm to 0.1mm for file format */
		dx = roundDouble(pointer->stitch.xx * 10.0) - xx;
		dy = roundDouble(pointer->stitch.yy * 10.0) - yy;
		xx = roundDouble(pointer->stitch.xx * 10.0);
		yy = roundDouble(pointer->stitch.yy * 10.0);
		flags = pointer->stitch.flags;
		encode_record(file, dx, dy, flags);
		pointer = pointer->next;
	}
	embFile_close(file);
	return 1;
}
static void pecEncode(EmbFile* file, EmbPattern* p)
{
    double thisX = 0.0;
    double thisY = 0.0;
    unsigned char stopCode = 2;
    EmbStitchList* list = 0;

    if(!file) { embLog_error("format-pec.c pecEncode(), file argument is null\n"); return; }
    if(!p) { embLog_error("format-pec.c pecEncode(), p argument is null\n"); return; }

    list = p->stitchList;
    while(list)
    {
        int deltaX, deltaY;
        EmbStitch s = list->stitch;

        deltaX = roundDouble(s.xx - thisX);
        deltaY = roundDouble(s.yy - thisY);
        thisX += (double)deltaX;
        thisY += (double)deltaY;

        if(s.flags & STOP)
        {
            pecEncodeStop(file, stopCode);
            if(stopCode == (unsigned char)2)
            {
                stopCode = (unsigned char)1;
            }
            else
            {
                stopCode = (unsigned char)2;
            }
        }
        else if(s.flags & END)
        {
            binaryWriteByte(file, 0xFF);
            break;
        }
        else if(deltaX < 63 && deltaX > -64 && deltaY < 63 && deltaY > -64 && (!(s.flags & (JUMP | TRIM))))
        {
            binaryWriteByte(file, (deltaX < 0) ? (unsigned char)(deltaX + 0x80) : (unsigned char)deltaX);
            binaryWriteByte(file, (deltaY < 0) ? (unsigned char)(deltaY + 0x80) : (unsigned char)deltaY);
        }
        else
        {
            pecEncodeJump(file, deltaX, s.flags);
            pecEncodeJump(file, deltaY, s.flags);
        }
        list = list->next;
    }
}
Beispiel #6
0
static void xxxEncodeStitch(FILE* file, double deltaX, double deltaY, int flags)
{
    if((flags & (JUMP | TRIM)) && (fabs(deltaX) > 124 || fabs(deltaY) > 124))
    {
        binaryWriteByte(file, 0x7E);
        binaryWriteShort(file, (short)deltaX);
        binaryWriteShort(file, (short)deltaY);
    }
    else
    {
        /* TODO: Verify this works after changing this to unsigned char */
        binaryWriteByte(file, (unsigned char)roundDouble(deltaX));
        binaryWriteByte(file, (unsigned char)roundDouble(deltaY));
    }
}
Beispiel #7
0
/*********************************************************
*NAME:          utilCalcTankSlide
*AUTHOR:        Chris Lesnieski
*CREATION DATE: 2009-01-04
*LAST MODIFIED: 2009-01-04
*PURPOSE:
*  Calculates the X and Y distance a tank should move
*  after being hit by a shell.  We do not decrement the
*  tank slide timer here as that is handled by code in the
*  tankUpdate() method.
*
*ARGUMENTS:
*  tankSlideTimer - The number of ticks left to slide
*  angle - The angle at which the shell was travelling
*  xAmount - The amount to add in the X direction
*  yAmount - The amount to add in the Y direction
*  speed   - The speed of the tank
*********************************************************/
void utilCalcTankSlide(BYTE tankSlideTimer, TURNTYPE angle, int *xAmount, int *yAmount, int speed)
{
    float angleRad;
    float ratio;

    angleRad = mathConvertBradianToRadian(angle);

    /* This ratio is not right as it is but it sure is fun to play with */
    ratio = (float)tankSlideTimer / (float)speed;

    /* Calculate the change in both x and y directions */
    *xAmount = (int) roundDouble((ratio * cos(angleRad)));
    *yAmount = (int) roundDouble((ratio * sin(angleRad)) * -1);

    return;
}
int loadImageSDRAM(void* sdram_ptr, int num)
{
	char filename[40];
	snprintf(filename, sizeof(filename), "Test Data/image%d.csv", num);
	FILE* imgData = fopen(filename, "r");
	if(imgData == NULL)
	{
		printf("file open fail, check if \"Test Data/image%d.csv\" is a valid file\n", num);
		return -1;
	}
	int i, j;
	double value;
	char temp;
	unsigned short toStore;
	for(i = 0; i < 49; i++)
	{
		toStore = 0;
		for(j = 0; j < 16; j++)
		{
			fscanf(imgData, "%lf", &value);
			fscanf(imgData, "%c", &temp);
			toStore = toStore + (unsigned short)(roundDouble(value) << j);
		}
		((unsigned short*) sdram_ptr)[i] = toStore;
		//printf("%d\n", toStore);
	}
	fclose(imgData);
	return 0;
}
Beispiel #9
0
/*********************************************************
*NAME:          utilCalcDistance
*AUTHOR:        John Morrison
*CREATION DATE: 25/12/98
*LAST MODIFIED: 25/12/98
*PURPOSE:
* Calculates the X and Y distance an object
* should move from a given speed and angle
*
*ARGUMENTS:
*  xAmount - The amount to add in the X direction
*  yAmount - The amount to add in the Y direction
*  angle   - The angle the tank is facing
*  speed   - The speed of the tank
*********************************************************/
void utilCalcDistance(int *xAmount, int *yAmount, TURNTYPE angle, int speed) {
    double dbAngle; /* Floating piont number for calculations */

    /* Take away 64 bradians to make angle correct for sin/cos calculations */
    angle -= BRADIANS_EAST;
    if (angle < 0) {
        angle += BRADIANS_MAX;
    }
    /* Convert bradians to degrees */
    dbAngle = (DEGREES_MAX / BRADIANS_MAX) * angle;
    /* Convert degrees to radians */
    dbAngle = (dbAngle / DEGREES_MAX) * RADIANS_MAX;
    /* Perform calculation */
    *xAmount = (int) roundDouble((speed * cos(dbAngle)));
    *yAmount = (int) roundDouble((speed * sin(dbAngle)));
}
int writeExp(EmbPattern* pattern, const char* fileName)
{
    FILE* file;
    EmbStitchList *stitches;
    double dx = 0.0, dy = 0.0;
    double xx = 0.0, yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char b[4];

    file = fopen(fileName, "wb");
    if(file == 0)
    {
        /*TODO: set status here "Error opening EXP file for write:" */
        return 0;
    }

    /* write stitches */
    stitches = pattern->stitchList;
    while(stitches)
    {
        dx = stitches->stitch.xx * 10.0 - xx;
        dy = stitches->stitch.yy * 10.0 - yy;
        xx = stitches->stitch.xx * 10.0;
        yy = stitches->stitch.yy * 10.0;
        flags = stitches->stitch.flags;
        expEncode(b, roundDouble(dx), roundDouble(dy), flags);
        if((b[0] == 128) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4)))
        {
            fprintf(file, "%c%c%c%c", b[0], b[1], b[2], b[3]);
        }
        else
        {
            fprintf(file, "%c%c", b[0], b[1]);
        }
        stitches = stitches->next;
    }
    fprintf(file, "\x1a");
    fclose(file);
    return 1;
}
/**
 * The synthetic video sequence we will work with here is composed of a
 * single moving object, circular in shape (fixed radius)
 * The motion here is a linear motion
 * the foreground intensity and the backgrounf intensity is known
 * the image is corrupted with zero mean Gaussian noise
 * @param I The video itself
 * @param IszX The x dimension of the video
 * @param IszY The y dimension of the video
 * @param Nfr The number of frames of the video
 * @param seed The seed array used for number generation
 */
void videoSequence(int * I, int IszX, int IszY, int Nfr, int * seed){
  int k;
  int max_size = IszX*IszY*Nfr;
  /*get object centers*/
  int x0 = (int)roundDouble(IszY/2.0);
  int y0 = (int)roundDouble(IszX/2.0);
  I[x0 *IszY *Nfr + y0 * Nfr  + 0] = 1;

  /*move point*/
  int xk, yk, pos;
  for(k = 1; k < Nfr; k++){
    xk = abs(x0 + (k-1));
    yk = abs(y0 - 2*(k-1));
    pos = yk * IszY * Nfr + xk *Nfr + k;
    if(pos >= max_size)
      pos = 0;
    I[pos] = 1;
  }

  /*dilate matrix*/
  int * newMatrix = (int *)malloc(sizeof(int)*IszX*IszY*Nfr);
  memset(newMatrix, 0, sizeof(int) * max_size);
  imdilate_disk(I, IszX, IszY, Nfr, 5, newMatrix);
  int x, y;
  for(x = 0; x < IszX; x++){
    for(y = 0; y < IszY; y++){
      for(k = 0; k < Nfr; k++){
        I[x*IszY*Nfr + y*Nfr + k] = newMatrix[x*IszY*Nfr + y*Nfr + k];
      }
    }
  }
  free(newMatrix);

  /*define background, add noise*/
  setIf(0, 100, I, &IszX, &IszY, &Nfr);
  setIf(1, 228, I, &IszX, &IszY, &Nfr);
  /*add noise*/
  addNoise(I, &IszX, &IszY, &Nfr, seed);
}
Beispiel #12
0
bool Player::setVolume(const QString& uuid, double volume)
{
  if (m_player)
  {
    double fake = 0.0;
    std::string _uuid = uuid.toUtf8().constData();
    for (RCTable::iterator it = m_RCTable.begin(); it != m_RCTable.end(); ++it)
    {
      if (it->uuid == _uuid)
      {
        int v = roundDouble(volume);
        if (!m_player->SetVolume(it->uuid, v))
          return false;
        it->volumeFake = (v == 0 ? 100.0 / 101.0 : volume);
        it->volume = v;
      }
      fake += it->volumeFake;
    }
    fake /= m_RCTable.size();
    m_RCGroup.volume = roundDouble(m_RCGroup.volumeFake = fake);
    emit renderingGroupChanged();
  }
  return false;
}
void loadWeightCSV(FILE* file, void* sdram_ptr, int sdram_offset, int rows, int cols)
{
	int i, j, k, l;
	double value;
	char temp;
	unsigned short toStore = 0;
	k = 0;
	l = 0;
	for(i = 0; i < rows; i++)
	{
		for(j = 0; j < cols; j++)
		{
			fscanf(file, "%lf", &value);
			fscanf(file, "%c", &temp);
			unsigned short num = 0;
			if(sdram_offset == W3_OFST)
			{
				value = value * 10;
			}
			int round = roundDouble(value);
			if(round < 0)
			{
				num = (round <= -8) ? 8 : convertBinPos(round);
			}
			else
			{
				num = (round > 7) ? 7 : round;
			}
			toStore = toStore + (num << (4 * k));
			if(k == 3 || (j == (cols - 1)))
			{
				((unsigned short*) sdram_ptr)[sdram_offset + l] = toStore;
				k = 0;
				toStore = 0;
				l++;
			}
			else
			{
				k++;
			}
		}
	}
}
void loadBiasCSV(FILE* file, void* sdram_ptr, int sdram_offset, int rows)
{
	int i, j, k;
	double value;
	char temp;
	unsigned short toStore = 0;
	unsigned short num = 0;
	j = 0;
	k = 0;
	for(i = 0; i < rows; i++)
	{
		fscanf(file, "%lf", &value);
		fscanf(file, "%c", &temp);
		int round = roundDouble(value);
		if(round < 0)
		{
			num = (round <= -8) ? 8 : convertBinPos(round);
		}
		else
		{
			num = (round >= 7.5) ? 7 : round;
		}
		toStore = toStore + (num << (j * 4));
		if(j == 3)
		{
			if(k == (rows / 4))
			{
				printf("*** LOAD BIAS GOING OUT OF BOUNDS ***\n");
			}
			((unsigned short*) sdram_ptr)[sdram_offset + k] = toStore;
			j = 0;
			toStore = 0;
			k++;
		}
		else
		{
			j++;
		}
	}
}
Beispiel #15
0
int writeHus(EmbPattern* pattern, const char* fileName)
{
    EmbRect boundingRect;
    int stitchCount, minColors, patternColor;
    int attributeSize = 0;
    int xCompressedSize = 0;
    int yCompressedSize = 0;
    double previousX = 0;
    double previousY = 0;
    unsigned char* xValues, *yValues, *attributeValues;
    EmbStitchList* pointer = 0;
    double xx = 0.0;
    double yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char* attributeCompressed, *xCompressed, *yCompressed;
    FILE* file = 0;

    file = fopen(fileName, "wb");
    if(!file)
    {
        /*TODO: set status here "Error opening HUS file for write:" */
        return 0;
    }

    stitchCount = embStitchList_count(pattern->stitchList);
    /* embPattern_correctForMaxStitchLength(pattern, 0x7F, 0x7F); */
    minColors = embThreadList_count(pattern->threadList);
    patternColor = minColors;
    if(minColors > 24) minColors = 24;
    binaryWriteUInt(file, 0x00C8AF5B);
    binaryWriteUInt(file, stitchCount);
    binaryWriteUInt(file, minColors);

    boundingRect = embPattern_calcBoundingBox(pattern);
    binaryWriteShort(file, (short) roundDouble(boundingRect.right * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.top * 10.0 - 1.0));
    binaryWriteShort(file, (short) roundDouble(boundingRect.left * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.bottom * 10.0 - 1.0));

    binaryWriteUInt(file, 0x2A + 2 * minColors);

    xValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    /* TODO: malloc fail error */
    yValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    /* TODO: malloc fail error */
    attributeValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    /* TODO: malloc fail error */

    pointer = pattern->stitchList;
    while(pointer)
    {
        xx = pointer->stitch.xx;
        yy = pointer->stitch.yy;
        flags = pointer->stitch.flags;
        xValues[i] = husEncodeByte((xx - previousX) * 10.0);
        previousX = xx;
        yValues[i] = husEncodeByte((yy - previousY) * 10.0);
        previousY = yy;
        attributeValues[i] = husEncodeStitchType(flags);
        pointer = pointer->next;
        i++;
    }
    attributeCompressed = husCompressData(attributeValues, stitchCount, &attributeSize);
    xCompressed = husCompressData(xValues, stitchCount, &xCompressedSize);
    yCompressed = husCompressData(yValues, stitchCount, &yCompressedSize);
    /* TODO: error if husCompressData returns zero? */

    binaryWriteUInt(file, (unsigned int) (0x2A + 2 * patternColor + attributeSize));
    binaryWriteUInt(file, (unsigned int) (0x2A + 2 * patternColor + attributeSize + xCompressedSize));
    binaryWriteUInt(file, 0x00000000);
    binaryWriteUInt(file, 0x00000000);
    binaryWriteUShort(file, 0x0000);

    for(i = 0; i < patternColor; i++)
    {
        binaryWriteShort(file, (short)embThread_findNearestColorInArray(embThreadList_getAt(pattern->threadList, i).color, (EmbThread*)husThreads, husThreadCount));
    }

    binaryWriteBytes(file, (char*) attributeCompressed, attributeSize);
    binaryWriteBytes(file, (char*) xCompressed, xCompressedSize);
    binaryWriteBytes(file, (char*) yCompressed, yCompressedSize);

	free(xValues);
    free(xCompressed);
	free(yValues);
	free(yCompressed);
	free(attributeValues);
	free(attributeCompressed);

    fclose(file);
    return 1;
}
Beispiel #16
0
void Player::handleRenderingControlChange()
{
  if (m_player)
  {
#define RENDERING_UNCHANGED     0
#define RENDERING_GROUP_CHANGED 1
#define RENDERING_CHANGED       2
    unsigned signalMask = RENDERING_UNCHANGED;

    SONOS::SRPList props = m_player->GetRenderingProperty();
    // Setup fake volumes
    if (m_RCTable.empty())
    {
      double volume = 0.0;
      bool mute = true;
      SONOS::SRPList::const_iterator it = props.begin();
      while (it != props.end())
      {
        RCProperty item;
        item.uuid = it->uuid;
        item.name = it->subordinateName;
        item.mute = it->property.MuteMaster ? true : false;
        item.volume = it->property.VolumeMaster;
        item.volumeFake = it->property.VolumeMaster > 0 ? (double)it->property.VolumeMaster : 100.0 / 101.0;
        m_RCTable.push_back(item);
        if (!item.mute)
          mute = false; // exists active audio in group
        volume += item.volumeFake;
        ++it;
      }
      volume /= (double)props.size();
      m_RCGroup.volumeFake = volume;
      m_RCGroup.volume = roundDouble(volume);
      m_RCGroup.mute = mute;
      signalMask |= RENDERING_GROUP_CHANGED | RENDERING_CHANGED; // handles group & subordinate update
    }
    else
    {
      double volume = 0.0;
      bool mute = true;
      SONOS::SRPList::const_iterator it = props.begin();
      std::vector<RCProperty>::iterator itz = m_RCTable.begin();
      while (it != props.end())
      {
        bool _mute = it->property.MuteMaster ? true : false;
        if (_mute != itz->mute)
        {
          itz->mute = _mute;
          signalMask |= RENDERING_CHANGED;
        }
        if (it->property.VolumeMaster != itz->volume)
        {
          itz->volume = it->property.VolumeMaster;

          if (it->property.VolumeMaster > 0 && it->property.VolumeMaster < 100 &&
                  it->property.VolumeMaster != roundDouble(itz->volumeFake))
          {
            itz->volumeFake = (double)it->property.VolumeMaster;
            signalMask |= RENDERING_CHANGED;
          }
          else if (it->property.VolumeMaster == 100 && itz->volumeFake < 100.0)
          {
            itz->volumeFake = (double)it->property.VolumeMaster;
            signalMask |= RENDERING_CHANGED;
          }
          else if (it->property.VolumeMaster == 0 && itz->volumeFake >= 1.0)
          {
            itz->volumeFake = 100.0 / 101.0;
            signalMask |= RENDERING_CHANGED;
          }
        }
        SONOS::DBG(DBG_DEBUG, "%s: [%s] sig=%d volume: %3.3f [%d]\n", __FUNCTION__, it->uuid.c_str(), signalMask, itz->volumeFake, itz->volume);
        if (!itz->mute)
          mute = false; // exists active audio in group
        volume += itz->volumeFake;
        ++it;
        ++itz;
      }
      volume /= (double)props.size();
      m_RCGroup.volumeFake = volume;
      m_RCGroup.volume = roundDouble(volume);
      m_RCGroup.mute = mute;
      signalMask |= RENDERING_GROUP_CHANGED; // handles group update
    }

    SONOS::DBG(DBG_DEBUG, "%s: sig=%d volume: %3.3f [%d]\n", __FUNCTION__, signalMask, m_RCGroup.volumeFake, m_RCGroup.volume);

    // emit signal about changes
    if (signalMask & RENDERING_GROUP_CHANGED)
      emit renderingGroupChanged();
    if (signalMask & RENDERING_CHANGED)
      emit renderingChanged();
  }
}
/**
 * The implementation of the particle filter using OpenMP for many frames
 * @see http://openmp.org/wp/
 * @note This function is designed to work with a video of several frames. In addition, it references a provided MATLAB function which takes the video, the objxy matrix and the x and y arrays as arguments and returns the likelihoods
 * @param I The video to be run
 * @param IszX The x dimension of the video
 * @param IszY The y dimension of the video
 * @param Nfr The number of frames
 * @param seed The seed array used for random number generation
 * @param Nparticles The number of particles to be used
 */
int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, int Nparticles) {
    int max_size = IszX * IszY*Nfr;
    //original particle centroid
    double xe = roundDouble(IszY / 2.0);
    double ye = roundDouble(IszX / 2.0);

    //expected object locations, compared to center
    int radius = 5;
    int diameter = radius * 2 - 1;
    int * disk = (int*) calloc(diameter * diameter, sizeof (int));
    strelDisk(disk, radius);
    int countOnes = 0;
    int x, y;
    for (x = 0; x < diameter; x++) {
        for (y = 0; y < diameter; y++) {
            if (disk[x * diameter + y] == 1)
                countOnes++;
        }
    }
    int * objxy = (int *) calloc(countOnes * 2, sizeof(int));
    getneighbors(disk, countOnes, objxy, radius);
    //initial weights are all equal (1/Nparticles)
    double * weights = (double *) calloc(Nparticles, sizeof(double));
    for (x = 0; x < Nparticles; x++) {
        weights[x] = 1 / ((double) (Nparticles));
    }
    /****************************************************************
     **************   B E G I N   A L L O C A T E *******************
     ****************************************************************/

    /***** kernel variables ******/
    cl_kernel kernel_likelihood;
    cl_kernel kernel_sum;
    cl_kernel kernel_normalize_weights;
    cl_kernel kernel_find_index;

    int sourcesize = 2048 * 2048;
    char * source = (char *) calloc(sourcesize, sizeof (char));
    if (!source) {
        printf("ERROR: calloc(%d) failed\n", sourcesize);
        return -1;
    }

    // read the kernel core source
    char * tempchar = "./particle_double.cl";
    FILE * fp = fopen(tempchar, "rb");
    if (!fp) {
        printf("ERROR: unable to open '%s'\n", tempchar);
        return -1;
    }
    fread(source + strlen(source), sourcesize, 1, fp);
    fclose(fp);

    // OpenCL initialization
    int use_gpu = 1;
    if (initialize(use_gpu)) return -1;

    // compile kernel
    cl_int err = 0;
    const char * slist[2] = {source, 0};
    cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateProgramWithSource() => %d\n", err);
        return -1;
    }

    err = clBuildProgram(prog, 1, device_list, "-cl-fast-relaxed-math", NULL, NULL);

    if (err != CL_SUCCESS) {
        if (err == CL_INVALID_PROGRAM)
            printf("CL_INVALID_PROGRAM\n");
        else if (err == CL_INVALID_VALUE)
            printf("CL_INVALID_VALUE\n");
        else if (err == CL_INVALID_DEVICE)
            printf("CL_INVALID_DEVICE\n");
        else if (err == CL_INVALID_BINARY)
            printf("CL_INVALID_BINARY\n");
        else if (err == CL_INVALID_BUILD_OPTIONS)
            printf("CL_INVALID_BUILD_OPTIONS\n");
        else if (err == CL_INVALID_OPERATION)
            printf("CL_INVALID_OPERATION\n");
        else if (err == CL_COMPILER_NOT_AVAILABLE)
            printf("CL_COMPILER_NOT_AVAILABLE\n");
        else if (err == CL_BUILD_PROGRAM_FAILURE)
            printf("CL_BUILD_PROGRAM_FAILURE\n");
        else if (err == CL_INVALID_OPERATION)
            printf("CL_INVALID_OPERATION\n");
        else if (err == CL_OUT_OF_RESOURCES)
            printf("CL_OUT_OF_RESOURCES\n");
        else if (err == CL_OUT_OF_HOST_MEMORY)
            printf("CL_OUT_OF_HOST_MEMORY\n");

        printf("ERROR: clBuildProgram() => %d\n", err);

        static char log[65536];
        memset(log, 0, sizeof (log));

        err = clGetProgramBuildInfo(prog, device_list[0], CL_PROGRAM_BUILD_LOG, sizeof (log) - 1, log, NULL);
        if (err != CL_SUCCESS) {
            printf("ERROR: clGetProgramBuildInfo() => %d\n", err);
        }
        if (strstr(log, "warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);


    }
    // { // show warnings/errors
    //     static char log[65536];
    //     memset(log, 0, sizeof (log));
    //     cl_device_id device_id[2] = {0};
    //     err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof (device_id), device_id, NULL);
    //     if (err != CL_SUCCESS) {
    //         if (err == CL_INVALID_CONTEXT)
    //             printf("ERROR: clGetContextInfo() => CL_INVALID_CONTEXT\n");
    //         if (err == CL_INVALID_VALUE)
    //             printf("ERROR: clGetContextInfo() => CL_INVALID_VALUE\n");
    //     }
    // }//*/

    char * s_likelihood_kernel = "likelihood_kernel";
    char * s_sum_kernel = "sum_kernel";
    char * s_normalize_weights_kernel = "normalize_weights_kernel";
    char * s_find_index_kernel = "find_index_kernel";

    kernel_likelihood = clCreateKernel(prog, s_likelihood_kernel, &err);
    if (err != CL_SUCCESS) {
        if (err == CL_INVALID_PROGRAM)
            printf("ERROR: clCreateKernel(likelihood_kernel) 0 => INVALID PROGRAM %d\n", err);
        if (err == CL_INVALID_PROGRAM_EXECUTABLE)
            printf("ERROR: clCreateKernel(likelihood_kernel) 0 => INVALID PROGRAM EXECUTABLE %d\n", err);
        if (err == CL_INVALID_KERNEL_NAME)
            printf("ERROR: clCreateKernel(likelihood_kernel) 0 => INVALID KERNEL NAME %d\n", err);
        if (err == CL_INVALID_KERNEL_DEFINITION)
            printf("ERROR: clCreateKernel(likelihood_kernel) 0 => INVALID KERNEL DEFINITION %d\n", err);
        if (err == CL_INVALID_VALUE)
            printf("ERROR: clCreateKernel(likelihood_kernel) 0 => INVALID CL_INVALID_VALUE %d\n", err);
        printf("ERROR: clCreateKernel(likelihood_kernel) failed.\n");
        return -1;
    }
    kernel_sum = clCreateKernel(prog, s_sum_kernel, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateKernel(sum_kernel) 0 => %d\n", err);
        return -1;
    }
    kernel_normalize_weights = clCreateKernel(prog, s_normalize_weights_kernel, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateKernel(normalize_weights_kernel) 0 => %d\n", err);
        return -1;
    }
    kernel_find_index = clCreateKernel(prog, s_find_index_kernel, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateKernel(find_index_kernel) 0 => %d\n", err);
        return -1;
    }


    //initial likelihood to 0.0
    double * likelihood = (double *) calloc(Nparticles + 1, sizeof (double));
    double * arrayX = (double *) calloc(Nparticles, sizeof (double));
    double * arrayY = (double *) calloc(Nparticles, sizeof (double));
    double * xj = (double *) calloc(Nparticles, sizeof (double));
    double * yj = (double *) calloc(Nparticles, sizeof (double));
    double * CDF = (double *) calloc(Nparticles, sizeof(double));

    //GPU copies of arrays
    cl_mem arrayX_GPU;
    cl_mem arrayY_GPU;
    cl_mem xj_GPU;
    cl_mem yj_GPU;
    cl_mem CDF_GPU;
    cl_mem likelihood_GPU;
    cl_mem I_GPU;
    cl_mem weights_GPU;
    cl_mem objxy_GPU;

    int * ind = (int*) calloc(countOnes, sizeof(int));
    cl_mem ind_GPU;
    double * u = (double *) calloc(Nparticles, sizeof(double));
    cl_mem u_GPU;
    cl_mem seed_GPU;
    cl_mem partial_sums;


    //OpenCL memory allocation

    arrayX_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    arrayY_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    xj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer xj_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    yj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer yj_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    CDF_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) * Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer CDF_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    u_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer u_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    likelihood_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer likelihood_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    weights_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer weights_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    I_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (unsigned char) *IszX * IszY * Nfr, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer I_GPU (size:%d) => %d\n", IszX * IszY * Nfr, err);
        return -1;
    }
    objxy_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, 2*sizeof (int) *countOnes, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer objxy_GPU (size:%d) => %d\n", countOnes, err);
        return -1;
    }
    ind_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (int) *countOnes * Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer ind_GPU (size:%d) => %d\n", countOnes * Nparticles, err);
        return -1;
    }
    seed_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (int) *Nparticles, NULL, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer seed_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    partial_sums = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof (double) * Nparticles + 1, likelihood, &err);
    if (err != CL_SUCCESS) {
        printf("ERROR: clCreateBuffer partial_sums (size:%d) => %d\n", Nparticles, err);
        return -1;
    }

    //Donnie - this loop is different because in this kernel, arrayX and arrayY
    //  are set equal to xj before every iteration, so effectively, arrayX and
    //  arrayY will be set to xe and ye before the first iteration.
    for (x = 0; x < Nparticles; x++) {

        xj[x] = xe;
        yj[x] = ye;
    }

    int k;
    //double * Ik = (double *)calloc(IszX*IszY, sizeof(double));
    int indX, indY;
    //start send
    long long send_start = get_time();

    //OpenCL memory copy
    err = clEnqueueWriteBuffer(cmd_queue, I_GPU, 1, 0, sizeof (unsigned char) *IszX * IszY*Nfr, I, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer I_GPU (size:%d) => %d\n", IszX * IszY*Nfr, err);
        return -1;
    }
    err = clEnqueueWriteBuffer(cmd_queue, objxy_GPU, 1, 0, 2*sizeof (int) *countOnes, objxy, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer objxy_GPU (size:%d) => %d\n", countOnes, err);
        return -1;
    }
    err = clEnqueueWriteBuffer(cmd_queue, weights_GPU, 1, 0, sizeof (double) *Nparticles, weights, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer weights_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    err = clEnqueueWriteBuffer(cmd_queue, xj_GPU, 1, 0, sizeof (double) *Nparticles, xj, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    err = clEnqueueWriteBuffer(cmd_queue, yj_GPU, 1, 0, sizeof (double) *Nparticles, yj, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    err = clEnqueueWriteBuffer(cmd_queue, seed_GPU, 1, 0, sizeof (int) *Nparticles, seed, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: clEnqueueWriteBuffer seed_GPU (size:%d) => %d\n", Nparticles, err);
        return -1;
    }
    /**********************************************************************
     *********** E N D    A L L O C A T E ********************************
     *********************************************************************/

    long long send_end = get_time();
    printf("TIME TO SEND TO GPU: %f\n", elapsed_time(send_start, send_end));
    int num_blocks = ceil((double) Nparticles / (double) threads_per_block);
    printf("threads_per_block=%d \n",threads_per_block);
    size_t local_work[3] = {threads_per_block, 1, 1};
    size_t global_work[3] = {num_blocks*threads_per_block, 1, 1};

    for (k = 1; k < Nfr; k++) {
        /****************** L I K E L I H O O D ************************************/
        clSetKernelArg(kernel_likelihood, 0, sizeof (void *), (void*) &arrayX_GPU);
        clSetKernelArg(kernel_likelihood, 1, sizeof (void *), (void*) &arrayY_GPU);
        clSetKernelArg(kernel_likelihood, 2, sizeof (void *), (void*) &xj_GPU);
        clSetKernelArg(kernel_likelihood, 3, sizeof (void *), (void*) &yj_GPU);
        clSetKernelArg(kernel_likelihood, 4, sizeof (void *), (void*) &CDF_GPU);
        clSetKernelArg(kernel_likelihood, 5, sizeof (void *), (void*) &ind_GPU);
        clSetKernelArg(kernel_likelihood, 6, sizeof (void *), (void*) &objxy_GPU);
        clSetKernelArg(kernel_likelihood, 7, sizeof (void *), (void*) &likelihood_GPU);
        clSetKernelArg(kernel_likelihood, 8, sizeof (void *), (void*) &I_GPU);
        clSetKernelArg(kernel_likelihood, 9, sizeof (void *), (void*) &u_GPU);
        clSetKernelArg(kernel_likelihood, 10, sizeof (void *), (void*) &weights_GPU);
        clSetKernelArg(kernel_likelihood, 11, sizeof (cl_int), (void*) &Nparticles);
        clSetKernelArg(kernel_likelihood, 12, sizeof (cl_int), (void*) &countOnes);
        clSetKernelArg(kernel_likelihood, 13, sizeof (cl_int), (void*) &max_size);
        clSetKernelArg(kernel_likelihood, 14, sizeof (cl_int), (void*) &k);
        clSetKernelArg(kernel_likelihood, 15, sizeof (cl_int), (void*) &IszY);
        clSetKernelArg(kernel_likelihood, 16, sizeof (cl_int), (void*) &Nfr);
        clSetKernelArg(kernel_likelihood, 17, sizeof (void *), (void*) &seed_GPU);
        clSetKernelArg(kernel_likelihood, 18, sizeof (void *), (void*) &partial_sums);
        clSetKernelArg(kernel_likelihood, 19, threads_per_block * sizeof (double), NULL);

        //KERNEL FUNCTION CALL
        err = clEnqueueNDRangeKernel(cmd_queue, kernel_likelihood, 1, NULL, global_work, local_work, 0, 0, 0);
        clFinish(cmd_queue);
        if (err != CL_SUCCESS) {
            printf("ERROR: clEnqueueNDRangeKernel(kernel_likelihood)=>%d failed\n", err);
            //check_error(err, __FILE__, __LINE__);
            return -1;
        }
        /****************** E N D    L I K E L I H O O D **********************/
        /*************************** S U M ************************************/
        clSetKernelArg(kernel_sum, 0, sizeof (void *), (void*) &partial_sums);
        clSetKernelArg(kernel_sum, 1, sizeof (cl_int), (void*) &Nparticles);

        //KERNEL FUNCTION CALL
        err = clEnqueueNDRangeKernel(cmd_queue, kernel_sum, 1, NULL, global_work, local_work, 0, 0, 0);
        clFinish(cmd_queue);
        if (err != CL_SUCCESS) {
            printf("ERROR: clEnqueueNDRangeKernel(kernel_sum)=>%d failed\n", err);
            //check_error(err, __FILE__, __LINE__);
            return -1;
        }/*************************** E N D   S U M ****************************/



        /**************** N O R M A L I Z E     W E I G H T S *****************/
        clSetKernelArg(kernel_normalize_weights, 0, sizeof (void *), (void*) &weights_GPU);
        clSetKernelArg(kernel_normalize_weights, 1, sizeof (cl_int), (void*) &Nparticles);
        clSetKernelArg(kernel_normalize_weights, 2, sizeof (void *), (void*) &partial_sums); //*/
        clSetKernelArg(kernel_normalize_weights, 3, sizeof (void *), (void*) &CDF_GPU);
        clSetKernelArg(kernel_normalize_weights, 4, sizeof (void *), (void*) &u_GPU);
        clSetKernelArg(kernel_normalize_weights, 5, sizeof (void *), (void*) &seed_GPU);

        //KERNEL FUNCTION CALL
        err = clEnqueueNDRangeKernel(cmd_queue, kernel_normalize_weights, 1, NULL, global_work, local_work, 0, 0, 0);
        clFinish(cmd_queue);
        if (err != CL_SUCCESS) {
            printf("ERROR: clEnqueueNDRangeKernel(normalize_weights)=>%d failed\n", err);
            //check_error(err, __FILE__, __LINE__);
            return -1;
        }
        /************* E N D    N O R M A L I Z E     W E I G H T S ***********/

        //	ocl_print_double_array(cmd_queue, partial_sums, 40);
        // /********* I N T E R M E D I A T E     R E S U L T S ***************/
        // //OpenCL memory copying back from GPU to CPU memory
        err = clEnqueueReadBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (double) *Nparticles, arrayX, 0, 0, 0);
        err = clEnqueueReadBuffer(cmd_queue, arrayY_GPU, 1, 0, sizeof (double) *Nparticles, arrayY, 0, 0, 0);
        err = clEnqueueReadBuffer(cmd_queue, weights_GPU, 1, 0, sizeof (double) *Nparticles, weights, 0, 0, 0);

        xe = 0;
        ye = 0;
        double total=0.0;
        // estimate the object location by expected values
        for (x = 0; x < Nparticles; x++) {
            // if( 0.0000000 < arrayX[x]*weights[x]) printf("arrayX[%d]:%f, arrayY[%d]:%f, weights[%d]:%0.10f\n",x,arrayX[x], x, arrayY[x], x, weights[x]);
            //	printf("arrayX[%d]:%f | arrayY[%d]:%f | weights[%d]:%f\n",
            //		x, arrayX[x], x, arrayY[x], x, weights[x]);
            xe += arrayX[x] * weights[x];
            ye += arrayY[x] * weights[x];
            total+= weights[x];
        }
        printf("total weight: %lf\n", total);
        printf("XE: %lf\n", xe);
        printf("YE: %lf\n", ye);
        double distance = sqrt(pow((double) (xe - (int) roundDouble(IszY / 2.0)), 2) + pow((double) (ye - (int) roundDouble(IszX / 2.0)), 2));
        printf("%lf\n", distance);
        // /********* E N D    I N T E R M E D I A T E     R E S U L T S ***************/

        /******************** F I N D    I N D E X ****************************/
        //Set number of threads

        clSetKernelArg(kernel_find_index, 0, sizeof (void *), (void*) &arrayX_GPU);
        clSetKernelArg(kernel_find_index, 1, sizeof (void *), (void*) &arrayY_GPU);
        clSetKernelArg(kernel_find_index, 2, sizeof (void *), (void*) &CDF_GPU);
        clSetKernelArg(kernel_find_index, 3, sizeof (void *), (void*) &u_GPU);
        clSetKernelArg(kernel_find_index, 4, sizeof (void *), (void*) &xj_GPU);
        clSetKernelArg(kernel_find_index, 5, sizeof (void *), (void*) &yj_GPU);
        clSetKernelArg(kernel_find_index, 6, sizeof (void *), (void*) &weights_GPU);
        clSetKernelArg(kernel_find_index, 7, sizeof (cl_int), (void*) &Nparticles);
        //KERNEL FUNCTION CALL
        err = clEnqueueNDRangeKernel(cmd_queue, kernel_find_index, 1, NULL, global_work, local_work, 0, 0, 0);
        clFinish(cmd_queue);
        if (err != CL_SUCCESS) {
            printf("ERROR: clEnqueueNDRangeKernel(find_index)=>%d failed\n", err);
            //check_error(err, __FILE__, __LINE__);
            return -1;
        }
        /******************* E N D    F I N D    I N D E X ********************/

    }//end loop

    //block till kernels are finished
    //clFinish(cmd_queue);
    long long back_time = get_time();

    //OpenCL freeing of memory
    clReleaseProgram(prog);
    clReleaseMemObject(u_GPU);
    clReleaseMemObject(CDF_GPU);
    clReleaseMemObject(yj_GPU);
    clReleaseMemObject(xj_GPU);
    clReleaseMemObject(likelihood_GPU);
    clReleaseMemObject(I_GPU);
    clReleaseMemObject(objxy_GPU);
    clReleaseMemObject(ind_GPU);
    clReleaseMemObject(seed_GPU);
    clReleaseMemObject(partial_sums);

    long long free_time = get_time();

    //OpenCL memory copying back from GPU to CPU memory
    err = clEnqueueReadBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (double) *Nparticles, arrayX, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: Memcopy Out\n");
        return -1;
    }
    long long arrayX_time = get_time();
    err = clEnqueueReadBuffer(cmd_queue, arrayY_GPU, 1, 0, sizeof (double) *Nparticles, arrayY, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: Memcopy Out\n");
        return -1;
    }
    long long arrayY_time = get_time();
    err = clEnqueueReadBuffer(cmd_queue, weights_GPU, 1, 0, sizeof (double) *Nparticles, weights, 0, 0, 0);
    if (err != CL_SUCCESS) {
        printf("ERROR: Memcopy Out\n");
        return -1;
    }
    long long back_end_time = get_time();

    printf("GPU Execution: %lf\n", elapsed_time(send_end, back_time));
    printf("FREE TIME: %lf\n", elapsed_time(back_time, free_time));
    printf("SEND TO SEND BACK: %lf\n", elapsed_time(back_time, back_end_time));
    printf("SEND ARRAY X BACK: %lf\n", elapsed_time(free_time, arrayX_time));
    printf("SEND ARRAY Y BACK: %lf\n", elapsed_time(arrayX_time, arrayY_time));
    printf("SEND WEIGHTS BACK: %lf\n", elapsed_time(arrayY_time, back_end_time));

    xe = 0;
    ye = 0;
    // estimate the object location by expected values
    for (x = 0; x < Nparticles; x++) {
        xe += arrayX[x] * weights[x];
        ye += arrayY[x] * weights[x];
    }
    double distance = sqrt(pow((double) (xe - (int) roundDouble(IszY / 2.0)), 2) + pow((double) (ye - (int) roundDouble(IszX / 2.0)), 2));

    //Output results
    FILE *fid;
    fid=fopen("output.txt", "w+");
    if( fid == NULL ) {
        printf( "The file was not opened for writing\n" );
        return -1;
    }
    fprintf(fid, "XE: %lf\n", xe);
    fprintf(fid, "YE: %lf\n", ye);
    fprintf(fid, "distance: %lf\n", distance);
    fclose(fid);


    //OpenCL freeing of memory
    clReleaseMemObject(weights_GPU);
    clReleaseMemObject(arrayY_GPU);
    clReleaseMemObject(arrayX_GPU);

    //free regular memory
    free(likelihood);
    free(arrayX);
    free(arrayY);
    free(xj);
    free(yj);
    free(CDF);
    free(ind);
    free(u);
}
/**
 * The implementation of the particle filter using OpenMP for many frames
 * @see http://openmp.org/wp/
 * @note This function is designed to work with a video of several frames. In addition, it references a provided MATLAB function which takes the video, the objxy matrix and the x and y arrays as arguments and returns the likelihoods
 * @param I The video to be run
 * @param IszX The x dimension of the video
 * @param IszY The y dimension of the video
 * @param Nfr The number of frames
 * @param seed The seed array used for random number generation
 * @param Nparticles The number of particles to be used
 */
int particleFilter(int * I, int IszX, int IszY, int Nfr, int * seed, int Nparticles){

  int i, c;

#ifdef HW
  XFcuda xcore;
  int Status;


  Status = XFcuda_Initialize(&xcore, 0);
  if (Status != XST_SUCCESS) {
    printf("Initialization failed\n");
    return 1; // XST_FAILURE;
  }
#endif
  int max_size = IszX*IszY*Nfr;
  //long long start = get_time();
  //original particle centroid
  double xe = roundDouble(IszY/2.0);
  double ye = roundDouble(IszX/2.0);

  //expected object locations, compared to center
  int radius = 5;
  int diameter = radius*2 - 1;
  int * disk = (int *)malloc(diameter*diameter*sizeof(int));
  strelDisk(disk, radius);
  int countOnes = 0;
  int x, y;
  for(x = 0; x < diameter; x++){
    for(y = 0; y < diameter; y++){
      if(disk[x*diameter + y] == 1)
        countOnes++;
    }
  }
  double * objxy = (double *)malloc(countOnes*2*sizeof(double));
  getneighbors(disk, countOnes, objxy, radius);

  //long long get_neighbors = get_time();
  //printf("TIME TO GET NEIGHBORS TOOK: %f\n", elapsed_time(start, get_neighbors));
  //initial weights are all equal (1/Nparticles)
  double * weights = (double *)malloc(sizeof(double)*Nparticles);
  for(x = 0; x < Nparticles; x++){
    weights[x] = 1/((double)(Nparticles));
  }
  //long long get_weights = get_time();
  //printf("TIME TO GET WEIGHTSTOOK: %f\n", elapsed_time(get_neighbors, get_weights));
  //initial likelihood to 0.0
  //printf("%d\n", Nparticles);
  double * likelihood = (double *)malloc(sizeof(double)*Nparticles);
  double * arrayX = (double *)malloc(sizeof(double)*Nparticles);
  double * arrayY = (double *)malloc(sizeof(double)*Nparticles);
  double * xj = (double *)malloc(sizeof(double)*Nparticles);
  double * yj = (double *)malloc(sizeof(double)*Nparticles);
  double * CDF = (double *)malloc(sizeof(double)*Nparticles);

  //GPU copies of arrays
  //double * arrayX_GPU;
  //double * arrayY_GPU;
  //double * xj_GPU;
  //double * yj_GPU;
  //double * CDF_GPU;

  int * ind = (int*)malloc(sizeof(int)*countOnes);
  double * u = (double *)malloc(sizeof(double)*Nparticles);
  //double * u_GPU;

  //CUDA memory allocation
  //check_error(cudaMalloc((void **) &arrayX_GPU, sizeof(double)*Nparticles));
  //arrayX_GPU = (double*)malloc(sizeof(double)*Nparticles);
  //check_error(cudaMalloc((void **) &arrayY_GPU, sizeof(double)*Nparticles));
  //arrayY_GPU = (double*)malloc(sizeof(double)*Nparticles);
  //check_error(cudaMalloc((void **) &xj_GPU, sizeof(double)*Nparticles));
  //xj_GPU = (double*)malloc(sizeof(double)*Nparticles);
  //check_error(cudaMalloc((void **) &yj_GPU, sizeof(double)*Nparticles));
  //yj_GPU = (double*)malloc(sizeof(double)*Nparticles);
  //check_error(cudaMalloc((void **) &CDF_GPU, sizeof(double)*Nparticles));
  //CDF_GPU = (double*)malloc(sizeof(double)*Nparticles);
  //check_error(cudaMalloc((void **) &u_GPU, sizeof(double)*Nparticles));
  //u_GPU = (double*)malloc(sizeof(double)*Nparticles);
  for(x = 0; x < Nparticles; x++){
    arrayX[x] = xe;
    arrayY[x] = ye;
  }

  //Set number of threads
  int num_blocks = ceil((double) Nparticles/(double) threads_per_block);
  //printf("%d\n", num_blocks);
  dim3 grids, threads;
  grids.x = num_blocks;
  grids.y = 1;
  grids.z = 1;
  threads.x = threads_per_block;
  threads.y = 1;
  threads.z = 1;

#ifdef HW
  XFcuda_SetNparticles(&xcore, Nparticles);
  XFcuda_SetGriddim_x(&xcore, grids.x);
  XFcuda_SetGriddim_y(&xcore, grids.y);
  //XFcuda_SetGriddim_z(&xcore, grids.z);
  XFcuda_SetBlockdim_x(&xcore, threads.x);
  //XFcuda_SetBlockdim_y(&xcore, threads.y);
  //XFcuda_SetBlockdim_z(&xcore, threads.z);
  XFcuda_SetArrayx_addr(&xcore, (u32)arrayX / sizeof(double));
  XFcuda_SetArrayy_addr(&xcore, (u32)arrayY / sizeof(double));
  XFcuda_SetCdf_addr(&xcore, (u32)CDF / sizeof(double));
  XFcuda_SetU_addr(&xcore, (u32)u / sizeof(double));
  XFcuda_SetXj_addr(&xcore, (u32)xj / sizeof(double));
  XFcuda_SetYj_addr(&xcore, (u32)yj / sizeof(double));

#endif

  int k;
  //double * Ik = (double *)malloc(sizeof(double)*IszX*IszY);
  int indX, indY;
  double *result = (double *)malloc(3 * (Nfr - 1) * sizeof(double));
  i = 0;
  for(k = 1; k < Nfr; k++){
    //long long set_arrays = get_time();
    //printf("TIME TO SET ARRAYS TOOK: %f\n", elapsed_time(get_weights, set_arrays));
    //apply motion model
    //draws sample from motion model (random walk). The only prior information
    //is that the object moves 2x as fast as in the y direction

    for(x = 0; x < Nparticles; x++){
      arrayX[x] = arrayX[x] + 1.0 + 5.0*randn(seed, x);
      arrayY[x] = arrayY[x] - 2.0 + 2.0*randn(seed, x);
    }

    //particle filter likelihood
    //long long error = get_time();
    //printf("TIME TO SET ERROR TOOK: %f\n", elapsed_time(set_arrays, error));
    for(x = 0; x < Nparticles; x++){

      //compute the likelihood: remember our assumption is that you know
      // foreground and the background image intensity distribution.
      // Notice that we consider here a likelihood ratio, instead of
      // p(z|x). It is possible in this case. why? a hometask for you.		
      //calc ind
      for(y = 0; y < countOnes; y++){
        indX = roundDouble(arrayX[x]) + objxy[y*2 + 1];
        indY = roundDouble(arrayY[x]) + objxy[y*2];
        ind[y] = fabs(indX*IszY*Nfr + indY*Nfr + k);
        if(ind[y] >= max_size)
          ind[y] = 0;
      }
      likelihood[x] = calcLikelihoodSum(I, ind, countOnes);
      likelihood[x] = likelihood[x]/countOnes;
    }
    //long long likelihood_time = get_time();
    //printf("TIME TO GET LIKELIHOODS TOOK: %f\n", elapsed_time(error, likelihood_time));
    // update & normalize weights
    // using equation (63) of Arulampalam Tutorial		
    for(x = 0; x < Nparticles; x++){
      weights[x] = weights[x] * exp(likelihood[x]);
    }
    //long long exponential = get_time();
    //printf("TIME TO GET EXP TOOK: %f\n", elapsed_time(likelihood_time, exponential));
    double sumWeights = 0;	
    for(x = 0; x < Nparticles; x++){
      sumWeights += weights[x];
    }
    //long long sum_time = get_time();
    //printf("TIME TO SUM WEIGHTS TOOK: %f\n", elapsed_time(exponential, sum_time));
    for(x = 0; x < Nparticles; x++){
      weights[x] = weights[x]/sumWeights;
    }
    //long long normalize = get_time();
    //printf("TIME TO NORMALIZE WEIGHTS TOOK: %f\n", elapsed_time(sum_time, normalize));
    xe = 0;
    ye = 0;
    // estimate the object location by expected values
    for(x = 0; x < Nparticles; x++){
      //printf("%f %f %f\n", arrayX[x], arrayY[x], weights[x]);
      xe += arrayX[x] * weights[x];
      ye += arrayY[x] * weights[x];
    }
    //long long move_time = get_time();
    //printf("TIME TO MOVE OBJECT TOOK: %f\n", elapsed_time(normalize, move_time));
    //printf("XE: %lf\n", xe);
    //printf("YE: %lf\n", ye);
    double distance = sqrt( pow((double)(xe-(int)roundDouble(IszY/2.0)),2) + pow((double)(ye-(int)roundDouble(IszX/2.0)),2) );
    //printf("%lf\n", distance);
    result[i] = xe;
    result[i + 1] = ye;
    result[i + 2] = distance;
    i += 3;
    //display(hold off for now)

    //pause(hold off for now)

    //resampling


    CDF[0] = weights[0];
    for(x = 1; x < Nparticles; x++){
      CDF[x] = weights[x] + CDF[x-1];
    }
    //long long cum_sum = get_time();
    //printf("TIME TO CALC CUM SUM TOOK: %f\n", elapsed_time(move_time, cum_sum));
    double u1 = (1/((double)(Nparticles)))*randu(seed, 0);
    for(x = 0; x < Nparticles; x++){
      u[x] = u1 + x/((double)(Nparticles));
    }
    //long long u_time = get_time();
    //printf("TIME TO CALC U TOOK: %f\n", elapsed_time(cum_sum, u_time));
    //long long start_copy = get_time();
    //CUDA memory copying from CPU memory to GPU memory
    //cudaMemcpy(arrayX_GPU, arrayX, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(arrayX_GPU, arrayX, sizeof(double)*Nparticles);
    //cudaMemcpy(arrayY_GPU, arrayY, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(arrayY_GPU, arrayY, sizeof(double)*Nparticles);
    //cudaMemcpy(xj_GPU, xj, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(xj_GPU, xj, sizeof(double)*Nparticles);
    //cudaMemcpy(yj_GPU, yj, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(yj_GPU, yj, sizeof(double)*Nparticles);
    //cudaMemcpy(CDF_GPU, CDF, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(CDF_GPU, CDF, sizeof(double)*Nparticles);
    //cudaMemcpy(u_GPU, u, sizeof(double)*Nparticles, cudaMemcpyHostToDevice);
    //memcpy(u_GPU, u, sizeof(double)*Nparticles);
    //long long end_copy = get_time();
    //Xil_DCacheDisable();

#ifdef HW
    Xil_DCacheDisable();
    XFcuda_SetEn_fcuda1(&xcore, 1);
    XFcuda_Start(&xcore);
    while (!XFcuda_IsDone(&xcore));
    Xil_DCacheEnable();
#else
    //KERNEL FUNCTION CALL
    int j;
    for(j = 0; j < Nparticles; j++){
      x = findIndex(CDF, Nparticles, u[j]);
      if(x == -1)
        x = Nparticles-1;
      xj[j] = arrayX[x];
      yj[j] = arrayY[x];

    }
#endif
    //cudaThreadSynchronize();
    //long long start_copy_back = get_time();
    //CUDA memory copying back from GPU to CPU memory
    //cudaMemcpy(yj, yj_GPU, sizeof(double)*Nparticles, cudaMemcpyDeviceToHost);
    //memcpy(yj, yj_GPU, sizeof(double)*Nparticles);
    //cudaMemcpy(xj, xj_GPU, sizeof(double)*Nparticles, cudaMemcpyDeviceToHost);
    //memcpy(xj, xj_GPU, sizeof(double)*Nparticles);
    //long long end_copy_back = get_time();
    //printf("SENDING TO GPU TOOK: %lf\n", elapsed_time(start_copy, end_copy));
    //printf("CUDA EXEC TOOK: %lf\n", elapsed_time(end_copy, start_copy_back));
    //printf("SENDING BACK FROM GPU TOOK: %lf\n", elapsed_time(start_copy_back, end_copy_back));
    //long long xyj_time = get_time();
    //printf("TIME TO CALC NEW ARRAY X AND Y TOOK: %f\n", elapsed_time(u_time, xyj_time));

    for(x = 0; x < Nparticles; x++){
      //reassign arrayX and arrayY
      arrayX[x] = xj[x];
      arrayY[x] = yj[x];
      weights[x] = 1/((double)(Nparticles));
    }
    //long long reset = get_time();
    //printf("TIME TO RESET WEIGHTS TOOK: %f\n", elapsed_time(xyj_time, reset));
  }

  //CUDA freeing of memory
  /*cudaFree(u_GPU);
    cudaFree(CDF_GPU);
    cudaFree(yj_GPU);
    cudaFree(xj_GPU);
    cudaFree(arrayY_GPU);
    cudaFree(arrayX_GPU);*/
  /*
     free(u_GPU);
     free(CDF_GPU);
     free(yj_GPU);
     free(xj_GPU);
     free(arrayY_GPU);
     free(arrayX_GPU);
     */
  //free memory
  free(disk);
  free(objxy);
  free(weights);
  free(likelihood);
  free(arrayX);
  free(arrayY);
  free(xj);
  free(yj);
  free(CDF);
  free(u);
  free(ind);

  /*
     FILE *fp = fopen("cuda/gold_output_naive.txt", "r");
     if (fp == NULL) {
     printf("Cannot open file.\n");
     free(result);
     return 0;
     }
     char buffer[50];
     double gold_val;
     for (i = 0; i < 3 * (Nfr - 1); i++) {
     if (feof(fp)) {
     printf("Unexpected end of file.\n");
     free(result);
     return 0;
     }
     fgets(buffer, sizeof(buffer), fp);
     sscanf(buffer, "%lf\n", &gold_val);
     if (gold_val - result[i] < -EPSILON ||
     gold_val - result[i] > EPSILON) {

     printf("Mismatch result at %i: gold = %f, result = %f\n",
     i, gold_val, result[i]);
     free(result);
     return 0;
     }
     }
     free(result);
     */

#ifdef VERIFY
  for (i = 0; i < 3 * (Nfr - 1); i++) {
    printf("index %d: %f\n", i, result[i]);
  }

  for (i = 0; i < 3 * (Nfr - 1); i++) {
    if (fabs(gold_output[i] - result[i]) > EPSILON) {
      printf("Mismatch result at %i: gold = %f, result = %f\n",
          i, gold_output[i], result[i]);
      free(result);
      return 0;
    }
  }
#endif
  free(result);
  return 1;
}
Beispiel #19
0
void writePecStitches(EmbPattern* pattern, EmbFile* file, const char* fileName)
{
    EmbStitchList* tempStitches = 0;
    EmbRect bounds;
    unsigned char image[38][48];
    int i, flen, currentThreadCount, graphicsOffsetLocation, graphicsOffsetValue, height, width;
    double xFactor, yFactor;
    const char* forwardSlashPos = strrchr(fileName, '/');
    const char* backSlashPos = strrchr(fileName, '\\');
    const char* dotPos = strrchr(fileName, '.');
    const char* start = 0;

    if(!pattern) { embLog_error("format-pec.c writePecStitches(), pattern argument is null\n"); return; }
    if(!file) { embLog_error("format-pec.c writePecStitches(), file argument is null\n"); return; }
    if(!fileName) { embLog_error("format-pec.c writePecStitches(), fileName argument is null\n"); return; }

    if(forwardSlashPos)
    {
        start = forwardSlashPos + 1;
    }
    if(backSlashPos && backSlashPos > start)
    {
        start = backSlashPos + 1;
    }
    if(!start)
    {
        start = fileName;
    }
    binaryWriteBytes(file, "LA:", 3);
    flen = (int)(dotPos - start);

    while(start < dotPos)
    {
        binaryWriteByte(file, (unsigned char)*start);
        start++;
    }
    for(i = 0; i < (int)(16-flen); i++)
    {
        binaryWriteByte(file, (unsigned char)0x20);
    }
    binaryWriteByte(file, 0x0D);
    for(i = 0; i < 12; i++)
    {
        binaryWriteByte(file, (unsigned char)0x20);
    }
    binaryWriteByte(file, (unsigned char)0xFF);
    binaryWriteByte(file, (unsigned char)0x00);
    binaryWriteByte(file, (unsigned char)0x06);
    binaryWriteByte(file, (unsigned char)0x26);

    for(i = 0; i < 12; i++)
    {
        binaryWriteByte(file, (unsigned char)0x20);
    }
    currentThreadCount = embThreadList_count(pattern->threadList);
    binaryWriteByte(file, (unsigned char)(currentThreadCount-1));

    for(i = 0; i < currentThreadCount; i++)
    {
        binaryWriteByte(file, (unsigned char)embThread_findNearestColorInArray(embThreadList_getAt(pattern->threadList, i).color, (EmbThread*)pecThreads, pecThreadCount));
    }
    for(i = 0; i < (int)(0x1CF - currentThreadCount); i++)
    {
        binaryWriteByte(file, (unsigned char)0x20);
    }
    binaryWriteShort(file, (short)0x0000);

    graphicsOffsetLocation = embFile_tell(file);
    /* placeholder bytes to be overwritten */
    binaryWriteByte(file, (unsigned char)0x00);
    binaryWriteByte(file, (unsigned char)0x00);
    binaryWriteByte(file, (unsigned char)0x00);

    binaryWriteByte(file, (unsigned char)0x31);
    binaryWriteByte(file, (unsigned char)0xFF);
    binaryWriteByte(file, (unsigned char)0xF0);

    bounds = embPattern_calcBoundingBox(pattern);

    height = roundDouble(embRect_height(bounds));
    width = roundDouble(embRect_width(bounds));
    /* write 2 byte x size */
    binaryWriteShort(file, (short)width);
    /* write 2 byte y size */
    binaryWriteShort(file, (short)height);

    /* Write 4 miscellaneous int16's */
    binaryWriteShort(file, (short)0x1E0);
    binaryWriteShort(file, (short)0x1B0);

    binaryWriteUShortBE(file, (unsigned short)(0x9000 | -roundDouble(bounds.left)));
    binaryWriteUShortBE(file, (unsigned short)(0x9000 | -roundDouble(bounds.top)));

    pecEncode(file, pattern);
    graphicsOffsetValue = embFile_tell(file) - graphicsOffsetLocation + 2;
    embFile_seek(file, graphicsOffsetLocation, SEEK_SET);

    binaryWriteByte(file, (unsigned char)(graphicsOffsetValue & 0xFF));
    binaryWriteByte(file, (unsigned char)((graphicsOffsetValue >> 8) & 0xFF));
    binaryWriteByte(file, (unsigned char)((graphicsOffsetValue >> 16) & 0xFF));

    embFile_seek(file, 0x00, SEEK_END);

    /* Writing all colors */
    clearImage(image);
    tempStitches = pattern->stitchList;

    yFactor = 32.0 / height;
    xFactor = 42.0 / width;
    while(tempStitches->next)
    {
        int x = roundDouble((tempStitches->stitch.xx - bounds.left) * xFactor) + 3;
        int y = roundDouble((tempStitches->stitch.yy - bounds.top) * yFactor) + 3;
        image[y][x] = 1;
        tempStitches = tempStitches->next;
    }
    writeImage(file, image);

    /* Writing each individual color */
    tempStitches = pattern->stitchList;
    for(i = 0; i < currentThreadCount; i++)
    {
        clearImage(image);
        while(tempStitches->next)
        {
            int x = roundDouble((tempStitches->stitch.xx - bounds.left) * xFactor) + 3;
            int y = roundDouble((tempStitches->stitch.yy - bounds.top) * yFactor) + 3;
            if(tempStitches->stitch.flags & STOP)
            {
                tempStitches = tempStitches->next;
                break;
            }
            image[y][x] = 1;
            tempStitches = tempStitches->next;
        }
        writeImage(file, image);
    }
}
Beispiel #20
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writePcs(EmbPattern* pattern, const char* fileName)
{
    EmbStitchList* pointer = 0;
    EmbThreadList* threadPointer = 0;
    EmbFile* file = 0;
    int i = 0;
    unsigned char colorCount = 0;
    double xx = 0.0, yy = 0.0;

    if(!pattern) { embLog_error("format-pcs.c writePcs(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-pcs.c writePcs(), fileName argument is null\n"); return 0; }

    if(!embStitchList_count(pattern->stitchList))
    {
        embLog_error("format-pcs.c writePcs(), pattern contains no stitches\n");
        return 0;
    }

    /* Check for an END stitch and add one if it is not present */
    if(pattern->lastStitch->stitch.flags != END)
        embPattern_addStitchRel(pattern, 0, 0, END, 1);

    file = embFile_open(fileName, "wb");
    if(!file)
    {
        embLog_error("format-pcs.c writePcs(), cannot open %s for writing\n", fileName);
        return 0;
    }

    binaryWriteByte(file, (unsigned char)'2');
    binaryWriteByte(file, 3); /* TODO: select hoop size defaulting to Large PCS hoop */
    colorCount = (unsigned char)embThreadList_count(pattern->threadList);
    binaryWriteUShort(file, (unsigned short)colorCount);
    threadPointer = pattern->threadList;
    i = 0;
    while(threadPointer)
    {
        EmbColor color = threadPointer->thread.color;
        binaryWriteByte(file, color.r);
        binaryWriteByte(file, color.g);
        binaryWriteByte(file, color.b);
        binaryWriteByte(file, 0);
        threadPointer = threadPointer->next;
        i++;
    }

    for(; i < 16; i++)
    {
        binaryWriteUInt(file, 0); /* write remaining colors to reach 16 */
    }

    binaryWriteUShort(file, (unsigned short)embStitchList_count(pattern->stitchList));
    /* write stitches */
    xx = yy = 0;
    pointer = pattern->stitchList;
    while(pointer)
    {
        pcsEncode(file, roundDouble(pointer->stitch.xx * 10.0), roundDouble(pointer->stitch.yy * 10.0), pointer->stitch.flags);
        pointer = pointer->next;
    }
    embFile_close(file);
    return 1;
}
Beispiel #21
0
/**
 * setCountry(): Set the current country code.
 * @param newCountry New country code.
 */
void Options::setCountry(const int newCountry)
{
	unsigned char Reg_1[0x200];
	
	Flag_Clr_Scr = 1;
	
	Country = newCountry;
	switch (Country)
	{
		default:
		case -1:
			// Auto-detect.
			if (Genesis_Started || _32X_Started)
				Detect_Country_Genesis();
			else if (SegaCD_Started)
				Detect_Country_SegaCD();
			break;
		
		case 0:
			// Japan (NTSC)
			Game_Mode = 0;
			CPU_Mode = 0;
			break;
		
		case 1:
			// USA (NTSC)
			Game_Mode = 1;
			CPU_Mode = 0;
			break;
		
		case 2:
			// Europe (PAL)
			Game_Mode = 1;
			CPU_Mode = 1;
			break;
		
		case 3:
			// Japan (PAL)
			Game_Mode = 0;
			CPU_Mode = 1;
			break;
	}
	
	// TODO: Combine this with gens.cpp:Set_Clock_Freq().
	if (CPU_Mode)
	{
		CPL_Z80 = roundDouble((((double)CLOCK_PAL / 15.0) / 50.0) / 312.0);
		CPL_M68K = roundDouble((((double)CLOCK_PAL / 7.0) / 50.0) / 312.0);
		CPL_MSH2 = roundDouble(((((((double)CLOCK_PAL / 7.0) * 3.0) / 50.0) / 312.0) *
					(double)MSH2_Speed) / 100.0);
		CPL_SSH2 = roundDouble(((((((double)CLOCK_PAL / 7.0) * 3.0) / 50.0) / 312.0) *
					(double)SSH2_Speed) / 100.0);
		
		VDP_Num_Lines = 312;
		VDP_Status |= 0x0001;
		_32X_VDP.Mode &= ~0x8000;
		
		CD_Access_Timer = 2080;
		Timer_Step = 136752;
	}
	else
	{
		CPL_Z80 = roundDouble((((double)CLOCK_NTSC / 15.0) / 60.0) / 262.0);
		CPL_M68K = roundDouble((((double)CLOCK_NTSC / 7.0) / 60.0) / 262.0);
		CPL_MSH2 = roundDouble(((((((double)CLOCK_NTSC / 7.0) * 3.0) / 60.0) / 262.0) *
					(double)MSH2_Speed) / 100.0);
		CPL_SSH2 = roundDouble(((((((double) CLOCK_NTSC / 7.0) * 3.0) / 60.0) / 262.0) *
					(double)SSH2_Speed) / 100.0);
		
		VDP_Num_Lines = 262;
		VDP_Status &= 0xFFFE;
		_32X_VDP.Mode |= 0x8000;
		
		CD_Access_Timer = 2096;
		Timer_Step = 135708;
	}
	
	if (audio->enabled())
	{
		PSG_Save_State();
		YM2612_Save(Reg_1);
		
		audio->endSound();
		audio->setEnabled(false);
		
		if (CPU_Mode)
		{
			YM2612_Init(CLOCK_PAL / 7, audio->soundRate(), YM2612_Improv);
			PSG_Init(CLOCK_PAL / 15, audio->soundRate());
		}
		else
		{
			YM2612_Init(CLOCK_NTSC / 7, audio->soundRate(), YM2612_Improv);
			PSG_Init(CLOCK_NTSC / 15, audio->soundRate());
		}
		
		if (SegaCD_Started)
			Set_Rate_PCM(audio->soundRate());
		
		YM2612_Restore (Reg_1);
		PSG_Restore_State();
		
		if (!audio->initSound())
			return;
		
		audio->setEnabled(true);
		audio->playSound();
	}
	
	if (Game_Mode)
	{
		if (CPU_Mode)
			MESSAGE_L("Europe system (50 FPS)", "Europe system (50 FPS)", 1500);
		else
			MESSAGE_L("USA system (60 FPS)", "USA system (60 FPS)", 1500);
	}
	else
	{
		if (CPU_Mode)
			MESSAGE_L("Japan system (50 FPS)", "Japan system (50 FPS)", 1500);
		else
			MESSAGE_L("Japan system (60 FPS)", "Japan system (60 FPS)", 1500);
	}
	
	setGameName();
	return;
}
int main(void)
{
  srand( time( NULL ) );
  char op;
  double double1;
  double double2;
  int int1;
  int int2;

  printf( "%s\n", "add          ( + )   + double double" );
  printf( "%s\n", "subtract     ( - )   - double double" );
  printf( "%s\n", "multiply     ( * )   * double double" );
  printf( "%s\n", "divide       ( / )   / double double" );
  printf( "%s\n", "power        ( ^ )   ^ double int" );
  printf( "%s\n", "exponential  ( e )   e int" );
  printf( "%s\n", "factorial    ( ! )   ! int" );
  printf( "%s\n", "random range ( r )   r int int" );
  printf( "%s\n", "sum range    ( s )   s int int" );
  printf( "%s\n", "round        ( ~ )   ~ double" );
  printf( "%s\n", "roundup      ( ' )   ` double" );
  printf( "%s\n", "rounddown    ( _ )   _ double" );
  printf( "%s\n", "minimum      ( < )   < double double" );
  printf( "%s\n", "maximum      ( > )   > double double" );
  printf( "%s\n", "quit         ( q )   q" );
  
  do
  {
    scanf( " %c", &op );
    switch ( op )
    {
      case '+':
        scanf( "%lf%lf", &double1, &double2 );
        printf( "%.2f\n", double1 + double2 );
        break;
      
      case '-':
        scanf( "%lf%lf", &double1, &double2 );
        printf( "%.2f\n", double1 - double2 );
        break;

      case '*':
        scanf( "%lf%lf", &double1, &double2 );
        printf( "%.2f\n", double1 * double2 );
        break;
      
      case '/':
        scanf( "%lf%lf", &double1, &double2 );
        if ( double2 != 0 )
        {
          printf( "%.2f\n", double1 / double2 );
        }
        else
        {
          printf( "%s\n", "Error: Dividing by 0" );
        }
        break;
      
      case '^':
        scanf( "%lf%d", &double1, &int1 );
        printf( "%.4f\n", power( double1, int1 ) );
        break;

      case 'e':
        scanf( "%d", &int1 );
        printf( "%lf\n", power( 2.71828182846, int1 ) );
        break;
      
      case '!':
        scanf( "%d", &int1 );
        printf( "%d\n", factorial( int1 ) );
        break;

      case 'r':
        scanf( "%d%d", &int1, &int2 );
        printf( "%d\n", randomRange( int1, int2 ) );
        break;

      case 's':
        scanf( "%d%d", &int1, &int2 );
        printf( "%d\n", sumRange( int1, int2 ) );
        break;      

      case '~':
        scanf( "%lf", &double1 );
        printf( "%d\n", roundDouble( double1 ) );
        break;

      case '`':
        scanf( "%lf", &double1 );
        printf( "%d\n", (int) double1 + 1 );
        break;

      case '_':
        scanf( "%lf", &double1 );
        printf( "%d\n", (int) double1 );
        break;

      case '<':
        scanf( "%lf%lf", &double1, &double2 );
        if ( double1 < double2 )
        {
          printf( "%.2f\n", double1 );
        }
        else
        {
          printf( "%.2f\n", double2 );
        }
        break;

      case '>':
        scanf( "%lf%lf", &double1, &double2 );
        if ( double1 > double2 )
        {
          printf( "%.2f\n", double1 );
        }
        else
        {
          printf( "%.2f\n", double2 );
        }
        break;

      case 'q':
        printf( "%s\n", "Good-bye." );
        break;

      default:
        printf( "%s\n", "Invalid input." );
        break; 
    }
  }
  while(op !='q');
}
int writeVip(EmbPattern* pattern, const char* fileName)
{
    EmbRect boundingRect;
    int stitchCount, minColors, patternColor;
    int attributeSize = 0;
    int xCompressedSize = 0;
    int yCompressedSize = 0;
    double previousX = 0;
    double previousY = 0;
    unsigned char* xValues, *yValues, *attributeValues;
    EmbStitchList* pointer;
    double xx = 0.0;
    double yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char* attributeCompressed, *xCompressed, *yCompressed, *decodedColors, *encodedColors;
	unsigned char prevByte = 0;
	EmbThreadList *colorPointer;

    FILE* file = fopen(fileName, "wb");
    if(file == 0)
    {
        /*TODO: set status here "Error opening HUS file for write:" */
        return 0;
    }

    stitchCount = embStitch_count(pattern->stitchList);
	minColors = embThread_count(pattern->threadList);
	decodedColors = (unsigned char *) malloc(minColors* 4 *sizeof(unsigned char));
	encodedColors = (unsigned char *) malloc(minColors* 4 *sizeof(unsigned char));
    /* embPattern_correctForMaxStitchLength(pattern, 0x7F, 0x7F); */
    
    patternColor = minColors;
    if(minColors > 24) minColors = 24;

    binaryWriteUInt(file, 0x0190FC5D); 
    binaryWriteUInt(file, stitchCount);
    binaryWriteUInt(file, minColors);

    boundingRect = embPattern_calcBoundingBox(pattern);
    binaryWriteShort(file, (short) roundDouble(boundingRect.right * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.top * 10.0 - 1.0));
    binaryWriteShort(file, (short) roundDouble(boundingRect.left * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.bottom * 10.0 - 1.0));
	 
    binaryWriteUInt(file, 0x38 + (minColors << 3));

    xValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    yValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    attributeValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));

    pointer = pattern->stitchList;
    while(pointer)
    {
        xx = pointer->stitch.xx;
        yy = pointer->stitch.yy;
        flags = pointer->stitch.flags;
        xValues[i] = vipEncodeByte((xx - previousX) * 10.0);
        previousX = xx;
        yValues[i] = vipEncodeByte((yy - previousY) * 10.0);
        previousY = yy;
        attributeValues[i] = vipEncodeStitchType(flags);
        pointer = pointer->next;
        i++;
    }
    attributeCompressed = vipCompressData(attributeValues, stitchCount, &attributeSize);
    xCompressed = vipCompressData(xValues, stitchCount, &xCompressedSize);
    yCompressed = vipCompressData(yValues, stitchCount, &yCompressedSize);

    binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize));
    binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize + xCompressedSize));
    binaryWriteUInt(file, 0x00000000);
    binaryWriteUInt(file, 0x00000000);
    binaryWriteUShort(file, 0x0000);

	binaryWriteInt(file, minColors << 2);

	colorPointer = pattern->threadList;

    for (i = 0; i < minColors; i++)
    {
        int byteChunk = i << 2;
		EmbColor currentColor = colorPointer->thread.color;
		decodedColors[byteChunk] = currentColor.r;
        decodedColors[byteChunk + 1] = currentColor.g;
        decodedColors[byteChunk + 2] = currentColor.b;
        decodedColors[byteChunk + 3] = 0x01;
		colorPointer = colorPointer->next;
    }
    
    for (i = 0; i < minColors << 2; ++i)
    {
        unsigned char tmpByte = (unsigned char) (decodedColors[i] ^ vipDecodingTable[i]);
		prevByte = (unsigned char) (tmpByte ^ prevByte);
		binaryWriteByte(file, prevByte);
	}
	for (i = 0; i <= minColors; i++)
    {
        binaryWriteInt(file, 1);
    }
    binaryWriteUInt(file, 0); /* string length */
	binaryWriteShort(file, 0);
    binaryWriteBytes(file, (char*) attributeCompressed, attributeSize);
	binaryWriteBytes(file, (char*) xCompressed, xCompressedSize);
	binaryWriteBytes(file, (char*) yCompressed, yCompressedSize);
    fclose(file);
    return 1;
}
static unsigned char vipEncodeByte(double f)
{
    return (unsigned char)(int)roundDouble(f);
}
Beispiel #25
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeDst(EmbPattern* pattern, const char* fileName)
{
    EmbRect boundingRect;
    FILE* file = 0;
    int xx, yy, dx, dy, flags;
    int i;
    int co = 1, st = 0;
    int ax, ay, mx, my;
    char* pd = 0;
    EmbStitchList* pointer = 0;

    if(!pattern) { embLog_error("format-dst.c writeDst(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-dst.c writeDst(), fileName argument is null\n"); return 0; }

    file = fopen(fileName, "wb");
    if(!file)
    {
        embLog_error("format-dst.c writeDst(), cannot open %s for writing\n", fileName);
        return 0;
    }
    embPattern_correctForMaxStitchLength(pattern, 12.1, 12.1);

    xx = yy = 0;
    co = 1;
    co = embThreadList_count(pattern->threadList);
    st = 0;
    st = embStitchList_count(pattern->stitchList);
    flags = NORMAL;
    boundingRect = embPattern_calcBoundingBox(pattern);
    /* TODO: review the code below
    if (pattern->get_variable("design_name") != NULL)
    {
    char *la = stralloccopy(pattern->get_variable("design_name"));
    if (strlen(la)>16) la[16]='\0';

    fprintf(file,"LA:%-16s\x0d",la);
    free (la);
    }
    else
    {
    */
    fprintf(file, "LA:%-16s\x0d", "Untitled");
    /*} */
    fprintf(file, "ST:%7d\x0d", st);
    fprintf(file, "CO:%3d\x0d", co - 1); /* number of color changes, not number of colors! */
    fprintf(file, "+X:%5d\x0d", (int)(boundingRect.right * 10.0));
    fprintf(file, "-X:%5d\x0d", (int)(fabs(boundingRect.left) * 10.0));
    fprintf(file, "+Y:%5d\x0d", (int)(boundingRect.bottom * 10.0));
    fprintf(file, "-Y:%5d\x0d", (int)(fabs(boundingRect.top) * 10.0));


    ax = ay = mx = my = 0;
    /* TODO: review the code below */
    /*ax=pattern->get_variable_int("ax"); */ /* will return 0 if not defined */
    /*ay=pattern->get_variable_int("ay"); */
    /*mx=pattern->get_variable_int("mx"); */
    /*my=pattern->get_variable_int("my"); */

    /*pd=pattern->get_variable("pd");*/ /* will return null pointer if not defined */
    pd = 0;
    if(pd == 0 || strlen(pd) != 6)
    {
        /* pd is not valid, so fill in a default consisting of "******" */
        pd = "******";
    };
    fprintf(file, "AX:+%5d\x0d", ax);
    fprintf(file, "AY:+%5d\x0d", ay);
    fprintf(file, "MX:+%5d\x0d", mx);
    fprintf(file, "MY:+%5d\x0d", my);
    fprintf(file, "PD:%6s\x0d", pd);
    binaryWriteByte(file, 0x1a); /* 0x1a is the code for end of section. */

    /* pad out header to proper length */
    for(i = 125; i < 512; i++)
    {
        fprintf(file, " ");
    }

    /* write stitches */
    xx = yy = 0;
    pointer = pattern->stitchList;
    while(pointer)
    {
        /* convert from mm to 0.1mm for file format */
        dx = roundDouble(pointer->stitch.xx * 10.0) - xx;
        dy = roundDouble(pointer->stitch.yy * 10.0) - yy;
        xx = roundDouble(pointer->stitch.xx * 10.0);
        yy = roundDouble(pointer->stitch.yy * 10.0);
        flags = pointer->stitch.flags;
        encode_record(file, dx, dy, flags);
        pointer = pointer->next;
    }
    binaryWriteByte(file, 0xA1); /* finish file with a terminator character */
    binaryWriteShort(file, 0);
    fclose(file);
    return 1;
}
/**
* The implementation of the particle filter using OpenMP for many frames
* @see http://openmp.org/wp/
* @note This function is designed to work with a video of several frames. In addition, it references a provided MATLAB function which takes the video, the objxy matrix and the x and y arrays as arguments and returns the likelihoods
* @param I The video to be run
* @param IszX The x dimension of the video
* @param IszY The y dimension of the video
* @param Nfr The number of frames
* @param seed The seed array used for random number generation
* @param Nparticles The number of particles to be used
*/
void particleFilter(int * I, int IszX, int IszY, int Nfr, int * seed, int Nparticles){
	
	int max_size = IszX*IszY*Nfr;
	long long start = get_time();
	//original particle centroid
	double xe = roundDouble(IszY/2.0);
	double ye = roundDouble(IszX/2.0);
	
	//expected object locations, compared to center
	int radius = 5;
	int diameter = radius*2 - 1;
	int * disk = (int *)malloc(diameter*diameter*sizeof(int));
	strelDisk(disk, radius);
	int countOnes = 0;
	int x, y;
	for(x = 0; x < diameter; x++){
		for(y = 0; y < diameter; y++){
			if(disk[x*diameter + y] == 1)
				countOnes++;
		}
	}
	double * objxy = (double *)malloc(countOnes*2*sizeof(double));
	getneighbors(disk, countOnes, objxy, radius);
	
	long long get_neighbors = get_time();
	printf("TIME TO GET NEIGHBORS TOOK: %f\n", elapsed_time(start, get_neighbors));
	//initial weights are all equal (1/Nparticles)
	double * weights = (double *)malloc(sizeof(double)*Nparticles);
	#pragma omp parallel for shared(weights, Nparticles) private(x)
	for(x = 0; x < Nparticles; x++){
		weights[x] = 1/((double)(Nparticles));
	}
	long long get_weights = get_time();
	printf("TIME TO GET WEIGHTSTOOK: %f\n", elapsed_time(get_neighbors, get_weights));
	//initial likelihood to 0.0
	double * likelihood = (double *)malloc(sizeof(double)*Nparticles);
	double * arrayX = (double *)malloc(sizeof(double)*Nparticles);
	double * arrayY = (double *)malloc(sizeof(double)*Nparticles);
	double * xj = (double *)malloc(sizeof(double)*Nparticles);
	double * yj = (double *)malloc(sizeof(double)*Nparticles);
	double * CDF = (double *)malloc(sizeof(double)*Nparticles);
	double * u = (double *)malloc(sizeof(double)*Nparticles);
	int * ind = (int*)malloc(sizeof(int)*countOnes*Nparticles);
	#pragma omp parallel for shared(arrayX, arrayY, xe, ye) private(x)
	for(x = 0; x < Nparticles; x++){
		arrayX[x] = xe;
		arrayY[x] = ye;
	}
	int k;
	
	printf("TIME TO SET ARRAYS TOOK: %f\n", elapsed_time(get_weights, get_time()));
	int indX, indY;
	for(k = 1; k < Nfr; k++){
		long long set_arrays = get_time();
		//apply motion model
		//draws sample from motion model (random walk). The only prior information
		//is that the object moves 2x as fast as in the y direction
		#pragma omp parallel for shared(arrayX, arrayY, Nparticles, seed) private(x)
		for(x = 0; x < Nparticles; x++){
			arrayX[x] += 1 + 5*randn(seed, x);
			arrayY[x] += -2 + 2*randn(seed, x);
		}
		long long error = get_time();
		printf("TIME TO SET ERROR TOOK: %f\n", elapsed_time(set_arrays, error));
		//particle filter likelihood
		#pragma omp parallel for shared(likelihood, I, arrayX, arrayY, objxy, ind) private(x, y, indX, indY)
		for(x = 0; x < Nparticles; x++){
			//compute the likelihood: remember our assumption is that you know
			// foreground and the background image intensity distribution.
			// Notice that we consider here a likelihood ratio, instead of
			// p(z|x). It is possible in this case. why? a hometask for you.		
			//calc ind
			for(y = 0; y < countOnes; y++){
				indX = roundDouble(arrayX[x]) + objxy[y*2 + 1];
				indY = roundDouble(arrayY[x]) + objxy[y*2];
				ind[x*countOnes + y] = fabs(indX*IszY*Nfr + indY*Nfr + k);
				if(ind[x*countOnes + y] >= max_size)
					ind[x*countOnes + y] = 0;
			}
			likelihood[x] = 0;
			for(y = 0; y < countOnes; y++)
				likelihood[x] += (pow((I[ind[x*countOnes + y]] - 100),2) - pow((I[ind[x*countOnes + y]]-228),2))/50.0;
			likelihood[x] = likelihood[x]/((double) countOnes);
		}
		long long likelihood_time = get_time();
		printf("TIME TO GET LIKELIHOODS TOOK: %f\n", elapsed_time(error, likelihood_time));
		// update & normalize weights
		// using equation (63) of Arulampalam Tutorial
		#pragma omp parallel for shared(Nparticles, weights, likelihood) private(x)
		for(x = 0; x < Nparticles; x++){
			weights[x] = weights[x] * exp(likelihood[x]);
		}
		long long exponential = get_time();
		printf("TIME TO GET EXP TOOK: %f\n", elapsed_time(likelihood_time, exponential));
		double sumWeights = 0;
		#pragma omp parallel for private(x) reduction(+:sumWeights)
		for(x = 0; x < Nparticles; x++){
			sumWeights += weights[x];
		}
		long long sum_time = get_time();
		printf("TIME TO SUM WEIGHTS TOOK: %f\n", elapsed_time(exponential, sum_time));
		#pragma omp parallel for shared(sumWeights, weights) private(x)
		for(x = 0; x < Nparticles; x++){
			weights[x] = weights[x]/sumWeights;
		}
		long long normalize = get_time();
		printf("TIME TO NORMALIZE WEIGHTS TOOK: %f\n", elapsed_time(sum_time, normalize));
		xe = 0;
		ye = 0;
		// estimate the object location by expected values
		#pragma omp parallel for private(x) reduction(+:xe, ye)
		for(x = 0; x < Nparticles; x++){
			xe += arrayX[x] * weights[x];
			ye += arrayY[x] * weights[x];
		}
		long long move_time = get_time();
		printf("TIME TO MOVE OBJECT TOOK: %f\n", elapsed_time(normalize, move_time));
		printf("XE: %lf\n", xe);
		printf("YE: %lf\n", ye);
		double distance = sqrt( pow((double)(xe-(int)roundDouble(IszY/2.0)),2) + pow((double)(ye-(int)roundDouble(IszX/2.0)),2) );
		printf("%lf\n", distance);
		//display(hold off for now)
		
		//pause(hold off for now)
		
		//resampling
		
		
		CDF[0] = weights[0];
		for(x = 1; x < Nparticles; x++){
			CDF[x] = weights[x] + CDF[x-1];
		}
		long long cum_sum = get_time();
		printf("TIME TO CALC CUM SUM TOOK: %f\n", elapsed_time(move_time, cum_sum));
		double u1 = (1/((double)(Nparticles)))*randu(seed, 0);
		#pragma omp parallel for shared(u, u1, Nparticles) private(x)
		for(x = 0; x < Nparticles; x++){
			u[x] = u1 + x/((double)(Nparticles));
		}
		long long u_time = get_time();
		printf("TIME TO CALC U TOOK: %f\n", elapsed_time(cum_sum, u_time));
		int j, i;
		
		#pragma omp parallel for shared(CDF, Nparticles, xj, yj, u, arrayX, arrayY) private(i, j)
		for(j = 0; j < Nparticles; j++){
			i = findIndex(CDF, Nparticles, u[j]);
			if(i == -1)
				i = Nparticles-1;
			xj[j] = arrayX[i];
			yj[j] = arrayY[i];
			
		}
		long long xyj_time = get_time();
		printf("TIME TO CALC NEW ARRAY X AND Y TOOK: %f\n", elapsed_time(u_time, xyj_time));
		//reassign arrayX and arrayY
		arrayX = xj;
		arrayY = yj;
		//#pragma omp parallel for shared(weights, Nparticles) private(x)
		for(x = 0; x < Nparticles; x++){
			weights[x] = 1/((double)(Nparticles));
		}
		long long reset = get_time();
		printf("TIME TO RESET WEIGHTS TOOK: %f\n", elapsed_time(xyj_time, reset));
	}
	free(disk);
	free(objxy);
	free(weights);
	free(likelihood);
	free(arrayX);
	free(arrayY);
	free(CDF);
	free(u);
	free(ind);
}
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeVip(EmbPattern* pattern, const char* fileName)
{
    EmbRect boundingRect;
    int stitchCount, minColors, patternColor;
    int attributeSize = 0;
    int xCompressedSize = 0;
    int yCompressedSize = 0;
    double previousX = 0;
    double previousY = 0;
    unsigned char* xValues = 0, *yValues = 0, *attributeValues = 0;
    EmbStitchList* pointer = 0;
    double xx = 0.0;
    double yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char* attributeCompressed = 0, *xCompressed = 0, *yCompressed = 0, *decodedColors = 0, *encodedColors = 0;
    unsigned char prevByte = 0;
    EmbThreadList* colorPointer = 0;
    EmbFile* file = 0;

    if(!pattern) { embLog_error("format-vip.c writeVip(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-vip.c writeVip(), fileName argument is null\n"); return 0; }

    stitchCount = embStitchList_count(pattern->stitchList);
    if(!stitchCount)
    {
        embLog_error("format-vip.c writeVip(), pattern contains no stitches\n");
        return 0;
    }

    /* Check for an END stitch and add one if it is not present */
    if(pattern->lastStitch && pattern->lastStitch->stitch.flags != END)
    {
        embPattern_addStitchRel(pattern, 0, 0, END, 1);
        stitchCount++;
    }

    file = embFile_open(fileName, "wb");
    if(file == 0)
    {
        embLog_error("format-vip.c writeVip(), cannot open %s for writing\n", fileName);
        return 0;
    }

    minColors = embThreadList_count(pattern->threadList);
    decodedColors = (unsigned char*)malloc(minColors << 2);
    if(!decodedColors) return 0;
    encodedColors = (unsigned char*)malloc(minColors << 2);
    if(encodedColors) /* TODO: review this line. It looks clearly wrong. If not, note why. */
    {
        free(decodedColors);
        decodedColors = 0;
        return 0;
    }
    /* embPattern_correctForMaxStitchLength(pattern, 0x7F, 0x7F); */

    patternColor = minColors;
    if(minColors > 24) minColors = 24;

    binaryWriteUInt(file, 0x0190FC5D);
    binaryWriteUInt(file, stitchCount);
    binaryWriteUInt(file, minColors);

    boundingRect = embPattern_calcBoundingBox(pattern);
    binaryWriteShort(file, (short) roundDouble(boundingRect.right * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.top * 10.0 - 1.0));
    binaryWriteShort(file, (short) roundDouble(boundingRect.left * 10.0));
    binaryWriteShort(file, (short) -roundDouble(boundingRect.bottom * 10.0 - 1.0));

    binaryWriteUInt(file, 0x38 + (minColors << 3));

    xValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    yValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    attributeValues = (unsigned char*)malloc(sizeof(unsigned char)*(stitchCount));
    if(xValues && yValues && attributeValues)
    {
        pointer = pattern->stitchList;
        while(pointer)
        {
            xx = pointer->stitch.xx;
            yy = pointer->stitch.yy;
            flags = pointer->stitch.flags;
            xValues[i] = vipEncodeByte((xx - previousX) * 10.0);
            previousX = xx;
            yValues[i] = vipEncodeByte((yy - previousY) * 10.0);
            previousY = yy;
            attributeValues[i] = vipEncodeStitchType(flags);
            pointer = pointer->next;
            i++;
        }
        attributeCompressed = vipCompressData(attributeValues, stitchCount, &attributeSize);
        xCompressed = vipCompressData(xValues, stitchCount, &xCompressedSize);
        yCompressed = vipCompressData(yValues, stitchCount, &yCompressedSize);

        binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize));
        binaryWriteUInt(file, (unsigned int) (0x38 + (minColors << 3) + attributeSize + xCompressedSize));
        binaryWriteUInt(file, 0x00000000);
        binaryWriteUInt(file, 0x00000000);
        binaryWriteUShort(file, 0x0000);

        binaryWriteInt(file, minColors << 2);

        colorPointer = pattern->threadList;

        for(i = 0; i < minColors; i++)
        {
            int byteChunk = i << 2;
            EmbColor currentColor = colorPointer->thread.color;
            decodedColors[byteChunk] = currentColor.r;
            decodedColors[byteChunk + 1] = currentColor.g;
            decodedColors[byteChunk + 2] = currentColor.b;
            decodedColors[byteChunk + 3] = 0x01;
            colorPointer = colorPointer->next;
        }

        for(i = 0; i < minColors << 2; ++i)
        {
            unsigned char tmpByte = (unsigned char) (decodedColors[i] ^ vipDecodingTable[i]);
            prevByte = (unsigned char) (tmpByte ^ prevByte);
            binaryWriteByte(file, prevByte);
        }
        for(i = 0; i <= minColors; i++)
        {
            binaryWriteInt(file, 1);
        }
        binaryWriteUInt(file, 0); /* string length */
        binaryWriteShort(file, 0);
        binaryWriteBytes(file, (char*) attributeCompressed, attributeSize);
        binaryWriteBytes(file, (char*) xCompressed, xCompressedSize);
        binaryWriteBytes(file, (char*) yCompressed, yCompressedSize);
    }

    if(attributeCompressed) { free(attributeCompressed); attributeCompressed = 0; }
    if(xCompressed) { free(xCompressed); xCompressed = 0; }
    if(yCompressed) { free(yCompressed); yCompressed = 0; }

    if(attributeValues) { free(attributeValues); attributeValues = 0; }
    if(xValues) { free(xValues); xValues = 0; }
    if(yValues) { free(yValues); yValues = 0; }

    if(decodedColors) { free(decodedColors); decodedColors = 0; }
    if(encodedColors) { free(encodedColors); encodedColors = 0; }

    embFile_close(file);
    return 1;
}