Example #1
0
int chunk_get_floats_endian_safe(Chunk* chunk, float* result, int nb) {
  int i = 0;
  float *src = (float*) (chunk->content + chunk->nb);
  int size = sizeof(float);
  if ((chunk->nb + (size * nb)) <= chunk->max) {
    for (i = 0; i < nb; i++) {
      result[i] = SWAP_FLOAT(src[i]);
    }
    chunk->nb += (nb * size);
    return 0;
  }
  printf("error in chunk_get_floats_endian_safe !\n");
  on_error();
  return 1;
}
Example #2
0
int chunk_add_floats_endian_safe(Chunk* chunk, float* ptr, int nb) {
  int i = 0;
  float *dest;
  int size = sizeof(float);
  int r = 0;
  if (chunk->max < (chunk->nb + (nb * size))) {
    r = chunk_size_up (chunk, (nb * size));
    if (r < 0) {
      printf("error in chunk_add_floats_endian_safe !\n");
      on_error();
      return 1;
    }
  }
  
  dest = (float*) (chunk->content + chunk->nb);
  for (i = 0; i < nb; i++) {
    dest[i] = SWAP_FLOAT(ptr[i]);
  }
  chunk->nb += (nb * size);
  return 0;
}
Example #3
0
int matout(char *fullname,char *varname,void *data,int nrows,int ncols,char vartype, char *mode, char endianess)
{
  int  size;
  long nelem;
  int  type,namelen;
  int  mrows,mcols,imagf;
  int i;
  FILE *fs;
  
  mrows = nrows;
  mcols = ncols;
  nelem = mrows*mcols;

  switch (vartype) {
  case 'd':   /* 8-byte doubles */
    type=00;
    size=8;
    double *vard=data;
    if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_DOUBLE(vard[i]); };
    break;
  case 'r':    /* 4-byte reals */
    type=10;
    size=4;
    float *varf=data;
    if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_FLOAT(varf[i]); };
    break;
  case 'l':    /* 4-byte int, row-wise */
    type=20;  
    size=4;
    int *varl=data;
    if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_INT(varl[i]); };
    break;
  case 's':    /* 2-byte signed shorts */
    type=30;
    size=2;
    short *vars=data;
    if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_SHORT(vars[i]); };
    break;
  case 'u':    /* 2-byte unsigned shorts */
    type=40;
    size=2;
    short *varus=data;
    if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_SHORT(varus[i]); };
    break;
    //  case 't':    /* 2-byte unsigned shorts saved as "text" */
    //    type=41;
    //    nbytes = mrows*mcols*2;
  case 't':    /* 1-byte unsigned shorts saved as "text" */
    type=51;
    size=1;
    break;
  case 'b':    /* 1-byte unsigned chars */
  case 'c':    /* 1-byte signed chars */
    type=50;
    size=1;
    break;
  default:
    return (-1);
  }

  if (disable_disk_writing) return (0);

  for (i=0 ; i<nfiles ; i++) {
    if (! *matfile[i]) break;
    if (!strcmp(fullname,matfile[i])) break;
  }
  if (i<nfiles) {
    fs = fd[i];
  } else {
    fs = NULL;
  }
  if (fs == NULL) {
    if ((fs=fopen(fullname,mode))==NULL) {
      sprintf(message,"%s could not be opened because of reason %d",fullname,errno);;
      warn(message);
      return (-1);
    }
    strcpy(matfile[i],fullname);
    fd[i] = fs;
    if ((i==nfiles) && (nfiles<MAXFILES-1)) nfiles++;
  }
  
  imagf=0;
  if (endianess=='B') SWAP_INT(type);
  if (fwrite(&type,sizeof(int),1,fs) != 1) {
    writerr();
    return (-1);
  }
  if (endianess=='B') SWAP_INT(mrows);
  if (fwrite(&mrows,sizeof(int),1,fs) != 1) {
    writerr();
    return (-1);
  }
  if (endianess=='B') SWAP_INT(mcols);
  if (fwrite(&mcols,sizeof(int),1,fs) != 1) {
    writerr();
    return (-1);
  }
  if (endianess=='B') SWAP_INT(imagf);
  if (fwrite(&imagf,sizeof(int),1,fs) != 1) {
    writerr();
    return (-1);
  }
    
  namelen=strlen(varname)+1;
  if (endianess=='B') SWAP_INT(namelen);
  if (fwrite(&namelen,sizeof(int),1,fs) != 1) {
    writerr();
    return (-1);
  }
  if (endianess=='B') SWAP_INT(namelen);
  if (fwrite(varname,(unsigned int)namelen,1,fs) != 1) {  
    writerr();
    return (-1);
  }

  if (fwrite(data,size,nelem,fs) != nelem) {
    writerr();
    return (-1);
  }

  matclose(fullname);
  
  return (0);
}
Example #4
0
void Y_ml4read(int nArgs)

{
  char *filename="";
  char *varname="";
  int leave_open = 0;
  
  if (nArgs==2) {
    filename=YGetString(sp-nArgs+1);
    varname=YGetString(sp-nArgs+2);
    leave_open = 0;
  } else if (nArgs==3) {
    filename=YGetString(sp-nArgs+1);
    varname=YGetString(sp-nArgs+2);
    leave_open=YGetInteger(sp-nArgs+3);
  }

  unsigned long bytes_read;
  int type,namelen;
  unsigned long nElements,nBytesToRead;
  int mrows,mcols,imagf;
  FILE *fs;
  int fileptr;
  int endian = 'L';
  int size=0,i;

  fs = openmat(filename);
  if (fs == NULL) YError(p_strncat("Can't open file ",filename,0));

  if (!matfind(fs,varname,50000)) YError(p_strncat("No Such variable ",varname,0));

  fileptr = ftell(fs);
  if (DEBUG) printf("@ position %d\n",fileptr);
  
  bytes_read = fread(&type,sizeof(int),1,fs);
  if (bytes_read==0) {
    matclose(filename);
    YError("Premature end of file");; // end of file
  }
  fread(&mrows,sizeof(int),1,fs);
  fread(&mcols,sizeof(int),1,fs);
  fread(&imagf,sizeof(int),1,fs);
    
  fread(&namelen,sizeof(int),1,fs);

  if (namelen & 0xffff0000) {
    if (DEBUG) printf("Big endian file\n");
    endian = 'B';
    SWAP_INT(type);
    SWAP_INT(mrows);
    SWAP_INT(mcols);
    SWAP_INT(imagf);
    SWAP_INT(namelen);
  }
  type = type%1000;

  if (DEBUG) printf("rows,cols,namelen= %d %d %d\n",mrows,mcols,namelen);

  if (namelen>255) {
    fseek(fs,fileptr,SEEK_SET);  // leave file ptr at begginning of this variable
    matclose(filename);
    YError("Variable name too long!");
  }

  fread(tempvarname,(unsigned int)namelen,1,fs);
  //  if ((*varname!='*') && strcmp(varname,tempvarname)) {  // error if not same varname
  if (!matchvarname(tempvarname,varname)) {  // error if not same varname
    fseek(fs,fileptr,SEEK_SET);  // leave file ptr at begginning of this variable
    matclose(filename);
    YError(p_strncat("Can't find variable",varname,0));
  }

  nElements = (unsigned)mrows*(unsigned)mcols;
  
  Dimension *tmp=tmpDims;
  tmpDims=0;
  FreeDimension(tmp);
  if (mrows<=1) {
    tmpDims= NewDimension(mcols, 1L, (Dimension *)0);
  } else if (mcols<=1) {
    tmpDims= NewDimension(mrows, 1L, (Dimension *)0);
  } else {
    tmpDims= NewDimension(mrows, 1L, (Dimension *)0);
    tmpDims= NewDimension(mcols, 1L, tmpDims);
  }
  
  if (type==0) {
    // 8-byte doubles
    size = 8;
    Array *a= PushDataBlock(NewArray(&doubleStruct, tmpDims));
    double *data = a->value.d;
    bytes_read = fread((void *)data,size,nElements,fs);
    if (endian=='B') { for (i=0;i<nElements;i++) SWAP_DOUBLE(data[i]); }

  } else if (type==10) {
    // 4-byte reals
    size = 4;
    Array *a= PushDataBlock(NewArray(&floatStruct, tmpDims));
    float *data = a->value.f;
    bytes_read = fread((void *)data,size,nElements,fs);
    if (endian=='B') { for (i=0;i<nElements;i++) SWAP_FLOAT(data[i]); }

  } else if ((type==120) || (type==20)) {
    // 4-byte int
    size = 4;
    Array *a= PushDataBlock(NewArray(&intStruct, tmpDims));
    int *data = a->value.l;
    bytes_read = fread((void *)data,size,nElements,fs);
    if (endian=='B') { for (i=0;i<nElements;i++) SWAP_INT(data[i]); }

  } else if (type==30) {
    // 2-byte signed (30) shorts
    size = 2;
    Array *a= PushDataBlock(NewArray(&shortStruct, tmpDims));
    short *data = a->value.s;
    bytes_read = fread((void *)data,size,nElements,fs);
    if (endian=='B') { for (i=0;i<nElements;i++) SWAP_SHORT(data[i]); }

  } else if (type==40) {
    // 2-byte unsigned (40) shorts
    size = 2;
    Array *a= PushDataBlock(NewArray(&shortStruct, tmpDims));
    short *data = a->value.s;
    Array *b= PushDataBlock(NewArray(&longStruct, tmpDims));
    long *data2 = b->value.l;
    bytes_read = fread((void *)data,size,nElements,fs);
    if (endian=='B') { for (i=0;i<nElements;i++) SWAP_SHORT(data[i]); }
    for (i=1;i<=nElements;i++) *(data2++) = (((long)*(data++))|0xFFFF0000)+65535;

  } else if (type==50) {
    // 1-byte signed or unsigned chars (50)
    size = 1;
    Array *a= PushDataBlock(NewArray(&charStruct, tmpDims));
    char *data = a->value.c;
    bytes_read = fread((void *)data,size,nElements,fs);

  } else if (type==51) {
    // text (51)
    size = 1;
    Array *a= PushDataBlock(NewArray(&stringStruct, (Dimension *)0));
    char *buf;
    a->value.q[0] = buf = p_malloc(nElements+1);
    if (DEBUG) printf("strlen: %d\n",(int)strlen((void *)a->value.q[0]));
    //    bytes_read = fread(a->value.q[0],1,nElements,fs);
    bytes_read = fread(buf,1,nElements,fs);
    *((char *)buf + nElements) = 0;  // append a NULL to text string

  } else {
    matclose(filename);
    sprintf(message,"Unknown type %d",type);
    YError(message);
  }

  if (bytes_read!=nElements) {
    fseek(fs,nElements*size,SEEK_CUR);
    matclose(filename);
    if (DEBUG) printf("read:%ld expected:%ld\n",bytes_read,nBytesToRead);
    YError("Premature end of file");
  }

  if (!leave_open) matclose(filename);
}
Example #5
0
static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args )
{
	VectorObject *line_a1, *line_a2, *line_b1, *line_b2;
	float a1x, a1y, a2x, a2y,  b1x, b1y, b2x, b2y, xi, yi, a1,a2,b1,b2, newvec[2];
	if( !PyArg_ParseTuple ( args, "O!O!O!O!",
	  &vector_Type, &line_a1,
	  &vector_Type, &line_a2,
	  &vector_Type, &line_b1,
	  &vector_Type, &line_b2)
	) {
		PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
		return NULL;
	}
	
	if(!BaseMath_ReadCallback(line_a1) || !BaseMath_ReadCallback(line_a2) || !BaseMath_ReadCallback(line_b1) || !BaseMath_ReadCallback(line_b2))
		return NULL;
	
	a1x= line_a1->vec[0];
	a1y= line_a1->vec[1];
	a2x= line_a2->vec[0];
	a2y= line_a2->vec[1];

	b1x= line_b1->vec[0];
	b1y= line_b1->vec[1];
	b2x= line_b2->vec[0];
	b2y= line_b2->vec[1];
	
	if((MIN2(a1x, a2x) > MAX2(b1x, b2x)) ||
	   (MAX2(a1x, a2x) < MIN2(b1x, b2x)) ||
	   (MIN2(a1y, a2y) > MAX2(b1y, b2y)) ||
	   (MAX2(a1y, a2y) < MIN2(b1y, b2y))  ) {
		Py_RETURN_NONE;
	}
	/* Make sure the hoz/vert line comes first. */
	if (fabs(b1x - b2x) < eul || fabs(b1y - b2y) < eul) {
		SWAP_FLOAT(a1x, b1x, xi); /*abuse xi*/
		SWAP_FLOAT(a1y, b1y, xi);
		SWAP_FLOAT(a2x, b2x, xi);
		SWAP_FLOAT(a2y, b2y, xi);
	}
	
	if (fabs(a1x-a2x) < eul) { /* verticle line */
		if (fabs(b1x-b2x) < eul){ /*verticle second line */
			Py_RETURN_NONE; /* 2 verticle lines dont intersect. */
		}
		else if (fabs(b1y-b2y) < eul) {
			/*X of vert, Y of hoz. no calculation needed */
			newvec[0]= a1x;
			newvec[1]= b1y;
			return newVectorObject(newvec, 2, Py_NEW, NULL);
		}
		
		yi = (float)(((b1y / fabs(b1x - b2x)) * fabs(b2x - a1x)) + ((b2y / fabs(b1x - b2x)) * fabs(b1x - a1x)));
		
		if (yi > MAX2(a1y, a2y)) {/* New point above seg1's vert line */
			Py_RETURN_NONE;
		} else if (yi < MIN2(a1y, a2y)) { /* New point below seg1's vert line */
			Py_RETURN_NONE;
		}
		newvec[0]= a1x;
		newvec[1]= yi;
		return newVectorObject(newvec, 2, Py_NEW, NULL);
	} else if (fabs(a2y-a1y) < eul) {  /* hoz line1 */
		if (fabs(b2y-b1y) < eul) { /*hoz line2*/
			Py_RETURN_NONE; /*2 hoz lines dont intersect*/
		}
		
		/* Can skip vert line check for seg 2 since its covered above. */
		xi = (float)(((b1x / fabs(b1y - b2y)) * fabs(b2y - a1y)) + ((b2x / fabs(b1y - b2y)) * fabs(b1y - a1y)));
		if (xi > MAX2(a1x, a2x)) { /* New point right of hoz line1's */
			Py_RETURN_NONE;
		} else if (xi < MIN2(a1x, a2x)) { /*New point left of seg1's hoz line */
			Py_RETURN_NONE;
		}
		newvec[0]= xi;
		newvec[1]= a1y;
		return newVectorObject(newvec, 2, Py_NEW, NULL);
	}
	
	b1 = (a2y-a1y)/(a2x-a1x);
	b2 = (b2y-b1y)/(b2x-b1x);
	a1 = a1y-b1*a1x;
	a2 = b1y-b2*b1x;
	
	if (b1 - b2 == 0.0) {
		Py_RETURN_NONE;
	}
	
	xi = - (a1-a2)/(b1-b2);
	yi = a1+b1*xi;
	if ((a1x-xi)*(xi-a2x) >= 0 && (b1x-xi)*(xi-b2x) >= 0 && (a1y-yi)*(yi-a2y) >= 0 && (b1y-yi)*(yi-b2y)>=0) {
		newvec[0]= xi;
		newvec[1]= yi;
		return newVectorObject(newvec, 2, Py_NEW, NULL);
	}
	Py_RETURN_NONE;
}