/*
* Routine: int DistNameIndex()
* Purpose: return the index of a column alias
* Algorithm:
* Data Structures:
*
* Params:	
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: 
*/
int
DistNameIndex(char *szDist, int nNameType, char *szName)
{
	d_idx_t *d_idx;
	dist_t *dist;
	int res;
	char *cp = NULL;
	
	if ((d_idx = find_dist(szDist)) == NULL)
		return(-1);	
	dist = d_idx->dist;
	
	if (dist->names == NULL)
		return(-1);

	res = 0;
	cp = dist->names;
	do {
		if (strcasecmp(szName, cp) == 0)
			break;
		cp += strlen(cp) + 1;
		res += 1;
	} while (res < (d_idx->v_width + d_idx->w_width));

	if (res >= 0)
	{
		if ((nNameType == VALUE_NAME) && (res < d_idx->v_width))
			return(res + 1);
		if ((nNameType == WEIGHT_NAME) && (res > d_idx->v_width))
			return(res - d_idx->v_width + 1);
	}

	return(-1);
}
int IntegrateDist(char *szDistName, int nPct, int nStartIndex, int nWeightSet)
{
	d_idx_t *pDistIndex;
	int nGoal,
		nSize;

	if ((nPct <= 0) || (nPct >= 100))
		return(QERR_RANGE_ERROR);
	
	pDistIndex=find_dist(szDistName);
	if (pDistIndex == NULL)
		return(QERR_BAD_NAME);

	if (nStartIndex > pDistIndex->length)
		return(QERR_RANGE_ERROR);

	nGoal = pDistIndex->dist->maximums[nWeightSet];
	nGoal = nGoal * nPct / 100;
	nSize = distsize(szDistName);

	while (nGoal >= 0)
	{
		nStartIndex++;
		nGoal -= dist_weight(NULL, szDistName, nStartIndex % nSize, nWeightSet);
	}
	
	return(nStartIndex);
}
/*
* Routine: int dist_weight
* Purpose: return the weight of a particular member of a distribution
* Algorithm:
* Data Structures:
*
* Params:	distribution *d
*			int index: which "row"
*			int wset: which set of weights
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: 
*	20000405 need to add error checking
*/
int
dist_weight(int *dest, char *d, int index, int wset)
{
	d_idx_t *d_idx;
	dist_t *dist;
	int res;
	
	if ((d_idx = find_dist(d)) == NULL)
	{
		char msg[80];
		sprintf(msg, "Invalid distribution name '%s'", d);
		INTERNAL(msg);
	}
	
	dist = d_idx->dist;
	
	res = dist->weight_sets[wset - 1][index - 1];
	/* reverse the accumulation of weights */
	if (index > 1)
		res -= dist->weight_sets[wset - 1][index - 2];
	
	if (dest == NULL)
		return(res);
	
	*dest = res;
	
	return(0);
}
/*
* Routine: int distsize(char *name)
* Purpose: return the size of a distribution
* Algorithm:
* Data Structures:
*
* Params:	
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: 
*	20000405 need to add error checking
*/
int
distsize(char *name)
{
	d_idx_t *dist;

	dist = find_dist(name);

	if (dist == NULL)
		return(-1);

	return(dist->length);
}
Example #5
0
int main()
{
	int i, j, k, h, t;
	real g, gg;
	//freopen("input.txt", "rt", stdin);
	scanf("%d%d", &m, &n);
	for (i = 0; i < m; i++) scanf(real_in, &point[i]);
	for (k = 0; k < n; k++) for (i = 0; i < m; i++)
	{
		scanf(real_in, &vec[k][i]);
	}
	sdist = calc_vec_slen(point);
	for (i = 0; i < n; i++) koeff[i] = 0;
	h = 1;
	for (i = 0; i < n; i++) h *= 3;	
	for (j = 0; j < h; j++)
	{
		for (t = 0; t < m; t++) p[t] = -point[t];
		k = j; w = 0;
		for (i = 0; i < n; i++)
		{
			cur_koeff[i] = 0;
			way[i] = k % 3; k /= 3;
			if (way[i] == 2)
			{
				for (t = 0; t < m; t++) a[w][t] = vec[i][t];
				where_koeff[w] = i;
				w++;
			}
			else if (way[i] == 1)
			{
				for (t = 0; t < m; t++)	p[t] -= vec[i][t];
				cur_koeff[i] = 1;
			}
		}
		find_dist();
	}
	printf(real_out"\n", rsqrt(sdist));
	for (i = 0; i < n; i++)
	{
		printf(real_out"%c", koeff[i], (i==n-1) ? '\n' : ' ');
	}
	gg = 0;
	for (i = 0; i < m; i++)
	{
		g = point[i];
		for (j = 0; j < n; j++) g += vec[j][i] * koeff[j];
		gg += g*g;
	}
	printf(real_out"\n", rsqrt(gg));
	return 0;
}
/*
* Routine: 
* Purpose: 
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: None
*/
void 
dump_dist(char *name)
{
	d_idx_t *pIndex;
	int i, j;
	char *pCharVal = NULL;
	int nVal;

	pIndex = find_dist(name);
	if (pIndex == NULL)
		ReportErrorNoLine(QERR_BAD_NAME, name, 1);
	printf("create %s;\n", pIndex->name);
	printf("set types = (");
	for (i=0; i < pIndex->v_width; i++)
	{
		if (i > 0)
			printf(", ");
		printf("%s", dist_type(name, i + 1) == 7?"int":"varchar");
	}
	printf(");\n");
	printf("set weights = %d;\n", pIndex->w_width);
	for (i=0; i < pIndex->length; i++)
	{
		printf("add(");
		for (j=0; j < pIndex->v_width; j++)
		{
			if (j)
				printf(", ");
			if (dist_type(name, j + 1) != 7)
			{
				dist_member(&pCharVal, name, i + 1, j + 1);
				printf("\"%s\"", pCharVal);
			}
			else
			{
				dist_member(&nVal, name, i + 1, j + 1);
				printf("%d", nVal);
			}
		}
		printf("; ");
		for (j=0; j < pIndex->w_width; j++)
		{
			if (j)
				printf(", ");
			printf("%d", dist_weight(NULL, name, i + 1, j + 1));
		}
		printf(");\n");
	}

	return;
}
/*
* Routine: int dist_type(char *name, int nValueSet)
* Purpose: return the type of the n-th value set in a distribution
* Algorithm:
* Data Structures:
*
* Params:	
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: 
*/
int
dist_type(char *name, int nValueSet)
{
	d_idx_t *dist;

	dist = find_dist(name);

	if (dist == NULL)
		return(-1);

	if (nValueSet < 1 || nValueSet > dist->v_width)
		return(-1);
	
	return(dist->dist->type_vector[nValueSet - 1]);
}
Example #8
0
ActionMove::ActionMove(MoveDirection direction, QPointF startPoint, QPointF endPoint)
{
	start = startPoint;
	end = endPoint;
	switch (direction) {
		case MOVE_FORWARD :
			name = "MoveForward";
			break;
		case MOVE_BACKWARD :
			name = "MoveBackward";
			break;
	}
	param = QString::number(find_dist());
	update_call();
}
/*
* Routine: DistSizeToShiftWidth(char *szDist) 
* Purpose: Determine the number of bits required to select a member of the distribution
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: None
*/
int DistSizeToShiftWidth(char *szDist, int nWeightSet)
{
	int nBits = 1,
		nTotal = 2,
		nMax;
	d_idx_t *d;

	d = find_dist(szDist);
	nMax = dist_max(d->dist, nWeightSet);
	
	while (nTotal < nMax)
	{
		nBits += 1;
		nTotal <<= 1;
	}

	return(nBits);
}
Example #10
0
bool Cluster::update()
{
  if (!Node::update()) return false;

  if (!isDirty(this, 40)) return true;
  
  boost::timer t1;

  cv::Mat in = getImage("in");
  if (in.empty()) return false;
	
  const bool use_manhat = getSignal("manhat") > 0.5;
  float max_space_dist = (in.cols*in.cols + in.rows*in.rows);
  if (use_manhat) max_space_dist = (in.cols + in.rows); 

  cv::Mat out = in.clone();
	/*
	 if (has_initted) {
		has_initted = true;
	 }
	 */
  const float margin = getSignal("margin");
  const float upper = 1.0 + margin;
  const float lower = 1.0 - margin;

	const float dist_weight = getSignal("dist_weight");

  int num = getSignal("num");
  // 1 just tracks the average color of the image
  if (num < 1) num = 1; 
  
  // TBD arbitrary maximum
  if (num > 10) num = 10;
  const int old_size = clusters.size();
  clusters.resize(num);

  std::vector<cluster_center> nc;
  nc.resize(clusters.size());
  for (int k = 0; k < nc.size(); k++) {
    initCluster(nc[k], in.cols, in.rows);

    if (k >= old_size) {
      initCluster(clusters[k], in.cols, in.rows, true); 
      LOG(INFO) << k << " " << old_size << " " << nc.size() << ", " << clusters[k].x;
    }
  }

  const bool wrap = getSignal("wrap") > 0.5;
  const int wd = in.cols;
  const int ht = in.rows;

  for (int y = 0; y < in.rows; ++y) {
  for (int x = 0; x < in.cols; ++x) {
    
    cv::Vec4b src2 = in.at<cv::Vec4b> (y,x);

	  float dist = max_space_dist;
	  int dist_ind = 0;

    // search through all clusters for nearest one
	  for (int k = 0; k < clusters.size(); k++) {
		  struct cluster_center cc = clusters[k];	
  
      int x2 = x;
      int y2 = y;

      if (wrap) {
        float dx = cc.x - x2;
	      float dy = cc.y - y2;

        if (dx + wd < -dx) x2 -= wd;
        else if ( -(dx - wd) < dx) x2 += wd;
        
        if (dy + ht < - y2) y2 -= ht;
        else if ( -(dy - ht) < dy) y2 += ht;

      }
    
      // There might be an inevitable amount of oscillation in wrap mode where
      // points that might be within range in multiple directions
      // will flip-flop between them.
      // could try to smooth motion of colors and centers
      const float span_x = (cc.max_x - cc.min_x)*(upper)+10;
      const float span_y = (cc.max_y - cc.min_y)*upper;
      const int mid_x = (cc.max_x + cc.min_x)/2;
      const int mid_y = (cc.max_y + cc.min_y)/2;
		  if ((x2 < mid_x + span_x/2) && (x2 > mid_x - span_x/2) &&
				  (y2 < mid_y + span_y/2) && (y2 > mid_y - span_y/2)) {

			  const float kdist = find_dist(
            src2.val[0],   src2.val[1],   src2.val[2],   x2, y2, 
					  cc.rgb.val[0], cc.rgb.val[1], cc.rgb.val[2], cc.x, cc.y,
					  max_space_dist, dist_weight,
            use_manhat);
            //in.cols, in.rows);
            //,
            //wrap); //, inst->color_weight);

        // store the closest match
			  if (kdist < dist) {
				  dist = kdist;
				  dist_ind = k;
			  }
		  }
	  } // clusters 

    // update min maxes
	  if (x > nc[dist_ind].max_x) nc[dist_ind].max_x = x;
	  if (x < nc[dist_ind].min_x) nc[dist_ind].min_x = x;
	  if (y > nc[dist_ind].max_y) nc[dist_ind].max_y = y;
	  if (y < nc[dist_ind].min_y) nc[dist_ind].min_y = y;

	  nc[dist_ind].aggr_x += x;
	  nc[dist_ind].aggr_y += y;
	  nc[dist_ind].aggr_r += src2.val[0];
	  nc[dist_ind].aggr_g += src2.val[1];
	  nc[dist_ind].aggr_b += src2.val[2];
	  nc[dist_ind].numpix += 1.0;

    // use the old cluster center color 
	  out.at<cv::Vec4b>(y,x) = clusters[dist_ind].rgb;
    // TBD optionally provide a scaled image that encodes distance from centers
	  //out.at<cv::Vec4b>(y,x) = cv::Vec4b(dist*1024, dist*512,
    //    dist*256, 0);
    //    clusters[dist_ind].rgb.val[2],0);

  }} // xy loop throug input image

  setImage("out", out);

  //setSignal("num", nc.size());
  /// update cluster_centers
  for (int k = 0; k < nc.size(); k++) {
    if (nc[k].numpix > 0) {
      nc[k].x = (int)  (nc[k].aggr_x/nc[k].numpix);
      nc[k].y = (int)  (nc[k].aggr_y/nc[k].numpix);
      nc[k].rgb = cv::Vec4b(
          (unsigned char) (nc[k].aggr_r/nc[k].numpix),
          (unsigned char) (nc[k].aggr_g/nc[k].numpix),
          (unsigned char) (nc[k].aggr_b/nc[k].numpix),
          0
          );
    }

    setSignal("x" + boost::lexical_cast<std::string>(k), nc[k].x);
    setSignal("y" + boost::lexical_cast<std::string>(k), nc[k].y);
    if (false) {
    setSignal("mnx" + boost::lexical_cast<std::string>(k), nc[k].min_x);
    setSignal("mny" + boost::lexical_cast<std::string>(k), nc[k].min_y);
    setSignal("mxx" + boost::lexical_cast<std::string>(k), nc[k].max_x);
    setSignal("mxy" + boost::lexical_cast<std::string>(k), nc[k].max_y);
    }
    setSignal("r" + boost::lexical_cast<std::string>(k), nc[k].rgb.val[0]);
    setSignal("g" + boost::lexical_cast<std::string>(k), nc[k].rgb.val[1]);
    setSignal("b" + boost::lexical_cast<std::string>(k), nc[k].rgb.val[2]);
    setSignal("p" + boost::lexical_cast<std::string>(k), nc[k].numpix);

  }

  clusters = nc;
    
  //setSignal("time", t1.elapsed());
}
Example #11
0
void f0r_update(f0r_instance_t instance, double time,
                const uint32_t* inframe, uint32_t* outframe)
{
  assert(instance);
  cluster_instance_t* inst = (cluster_instance_t*)instance;
  
  int x,y,k;
 
	float max_space_dist = sqrtf(inst->width*inst->width + inst->height*inst->height);

	/*
	 if (inst->has_initted) {
		inst->has_initted = true;
	 }
	 
	 */

  
  for (y=0; y < inst->height; ++y) {
  for (x=0; x < inst->width; ++x) {

	  const unsigned char* src2 = (unsigned char*)( &inframe[x+inst->width*y]);
	  unsigned char* dst2 		= (unsigned char*)(&outframe[x+inst->width*y]);

	  float dist = max_space_dist;
	  int dist_ind = 0;

	  for (k = 0; k < inst->num; k++) {
		  struct cluster_center cc = inst->clusters[k];	

		  float kdist = find_dist(src2[0], src2[1], src2[2], x,y, 
				  cc.r, cc.g, cc.b, cc.x, cc.y,
				  max_space_dist, inst->dist_weight); //, inst->color_weight);

		  if (kdist < dist) {
			  dist = kdist;
			  dist_ind = k;
		  }
	  }

	  struct cluster_center* cc = &inst->clusters[dist_ind];	
	  cc->aggr_x += x;
	  cc->aggr_y += y;
	  cc->aggr_r += src2[0];
	  cc->aggr_g += src2[1];
	  cc->aggr_b += src2[2];
	  cc->numpix += 1.0;

	  dst2[0] = cc->r;
	  dst2[1] = cc->g;
	  dst2[2] = cc->b;
	  dst2[3] = src2[3];

	

  }
  }

  /// update cluster_centers
  for (k = 0; k < inst->num; k++) {

	  struct cluster_center* cc = &inst->clusters[k];	

	  if (cc->numpix > 0) {
		  cc->x = (int)  (cc->aggr_x/cc->numpix);
		  cc->y = (int)  (cc->aggr_y/cc->numpix);
		  cc->r = (unsigned char) (cc->aggr_r/cc->numpix);
		  cc->g = (unsigned char) (cc->aggr_g/cc->numpix);
		  cc->b = (unsigned char) (cc->aggr_b/cc->numpix);

			//printf("%d, %d %d %d\t", k, (unsigned int)cc->r, (unsigned int)cc->g, (unsigned int)cc->b);
			//printf("%g, %g %g %g\n", cc->numpix, cc->aggr_r, cc->aggr_g, cc->aggr_b);
	  }
	
	  cc->numpix = 0;
	  cc->aggr_x = 0;
	  cc->aggr_y = 0;
	  cc->aggr_r = 0;
	  cc->aggr_g = 0;
	  cc->aggr_b = 0;

  	}
	//printf("\n");

}
/*
* Routine: 
* Purpose: 
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: None
*/
int MatchDistWeight(void *dest, char *szDist, int nWeight, int nWeightSet, int ValueSet)
{
		d_idx_t *d;
		dist_t *dist;
		int	index = 0,
			dt,
			i_res,
			nRetcode;
		char *char_val;
		
		if ((d = find_dist(szDist)) == NULL)
		{
			char msg[80];
			sprintf(msg, "Invalid distribution name '%s'", szDist);
			INTERNAL(msg);
		}
		
		dist = d->dist;
		nWeight %= dist->maximums[nWeightSet - 1];
		
		while (nWeight > dist->weight_sets[nWeightSet - 1][index] && 
			index < d->length)
			index += 1;
		dt = ValueSet - 1;
		if (index >= d->length)
			index = d->length - 1;
		char_val = dist->strings + dist->value_sets[dt][index];
		
		switch(dist->type_vector[dt])
		{
		case TKN_VARCHAR:
			if (dest)
				*(char **)dest = (char *)char_val;
			break;
		case TKN_INT:
			i_res = atoi(char_val);
			if (dest)
				*(int *)dest = i_res;
			break;
		case TKN_DATE:
			if (dest == NULL)
			{
				dest = (date_t *)malloc(sizeof(date_t));
				MALLOC_CHECK(dest);
			}
			strtodt(*(date_t **)dest, char_val);
			break;
		case TKN_DECIMAL:
			if (dest == NULL)
			{
				dest = (decimal_t *)malloc(sizeof(decimal_t));
				MALLOC_CHECK(dest);
			}
			strtodec(*(decimal_t **)dest,char_val);
			break;
		}
		
		nRetcode = 1;
		index = 1;
		while (index < dist->maximums[nWeightSet - 1])
		{
			nRetcode += 1;
			index *= 2;
		}

		return(nRetcode);
}
/*
* Routine: void *dist_op()
* Purpose: select a value/weight from a distribution
* Algorithm:
* Data Structures:
*
* Params:	char *d_name
*			int vset: which set of values
*			int wset: which set of weights
* Returns: appropriate data type cast as a void *
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: 20000317 Need to be sure this is portable to NT and others
*/
int
dist_op(void *dest, int op, char *d_name, int vset, int wset, int stream)
{
	d_idx_t *d;
	dist_t *dist;
	int	level,
		index = 0,
		dt;
	char *char_val;
	int i_res = 1;
	
	if ((d = find_dist(d_name)) == NULL)
	{
		char msg[80];
		sprintf(msg, "Invalid distribution name '%s'", d_name);
		INTERNAL(msg);
		assert(d != NULL);
	}
		
	dist = d->dist;
	
	if (op == 0)
	{
		genrand_integer(&level, DIST_UNIFORM, 1, 
			dist->maximums[wset - 1], 0, stream);
		while (level > dist->weight_sets[wset - 1][index] && 
			index < d->length)
			index += 1;
		dt = vset - 1;
		if ((index >= d->length) || (dt > d->v_width))
			INTERNAL("Distribution overrun");
		char_val = dist->strings + dist->value_sets[dt][index];
	}
	else
	{ 
		index = vset - 1;
		dt = wset - 1;
		if (index >= d->length || index < 0)
		{
			fprintf(stderr, "Runtime ERROR: Distribution over-run/under-run\n");
			fprintf(stderr, "Check distribution definitions and usage for %s.\n",
			d->name);
			fprintf(stderr, "index = %d, length=%d.\n",
				index, d->length);
			exit(1);
		}
		char_val = dist->strings + dist->value_sets[dt][index];
	}
	
	
	switch(dist->type_vector[dt])
	{
	case TKN_VARCHAR:
		if (dest)
			*(char **)dest = (char *)char_val;
		break;
	case TKN_INT:
		i_res = atoi(char_val);
		if (dest)
			*(int *)dest = i_res;
		break;
	case TKN_DATE:
		if (dest == NULL)
		{
			dest = (date_t *)malloc(sizeof(date_t));
			MALLOC_CHECK(dest);
		}
		strtodt(*(date_t **)dest, char_val);
		break;
	case TKN_DECIMAL:
		if (dest == NULL)
		{
			dest = (decimal_t *)malloc(sizeof(decimal_t));
			MALLOC_CHECK(dest);
		}
		strtodec(*(decimal_t **)dest,char_val);
		break;
	}
	
	return((dest == NULL)?i_res:index + 1);	/* shift back to the 1-based indexing scheme */
}
Example #14
0
void clust_align(int r,int l)
{
	int row,col,max = -9999999,len,h,k,i,j,nxt,temp;
	char al1[100],al2[100];

	

	for(i=0;i<cluster[r].tot_seq;i++)
	{
		for(j=0;j<cluster[l].tot_seq;j++)
		{
			temp = ned_wunz(cluster[r].seq_list[i],cluster[l].seq_list[j],al1,al2);
			if(temp>max)
			{
				max = temp;
				row = i;
				col = j;
			}
		}
	}

	
	ned_wunz(cluster[r].seq_list[row],cluster[l].seq_list[col],al1,al2);
	
	for(k=0;k<cluster[r].tot_seq;k++)
	{
		if(k==row)
			strcpy(cluster[num_clust].seq_list[k],al1);
		else
		{
			nxt =0;
			len =strlen(al1);
			for(h=0;h<len;h++)
			{
				if(al1[h]=='-')
					cluster[num_clust].seq_list[k][h]='-';
				else
				{
					cluster[num_clust].seq_list[k][h] = cluster[r].seq_list[k][nxt];
					nxt++;
				}
			}
			cluster[num_clust].seq_list[k][len]='\0';
		}
		
	}
	for(k=0;k<cluster[l].tot_seq;k++)
	{
		if(k==col)
			strcpy(cluster[num_clust].seq_list[k+cluster[r].tot_seq],al2);
		else
		{
			nxt =0;
			len =strlen(al2);
			for(h=0;h<len;h++)
			{
				if(al2[h]=='-')
					cluster[num_clust].seq_list[k+cluster[r].tot_seq][h]='-';
				else
				{
					cluster[num_clust].seq_list[k+cluster[r].tot_seq][h] = cluster[l].seq_list[k][nxt];
					nxt++;
				}
			}
			cluster[num_clust].seq_list[k+cluster[r].tot_seq][len]='\0';
		}
		
	}

	cluster[num_clust].tot_seq = cluster[l].tot_seq + cluster[r].tot_seq;

	

	for(i=0;i<cluster[num_clust].tot_seq;i++)
	{
		len = strlen(al1);
		for(j=0;j<len;j++)
			if(cluster[num_clust].seq_list[i][j]=='-')
				cluster[num_clust].seq_list[i][j] = 'X';
	}

	num_clust++;

	visit[r]=1;
	visit[l]=1;

	for(i=0;i<(num_clust-1);i++)
	{
		if(visit[i]!=1)
		{
			dismat[i][num_clust-1]=find_dist(r,l,i);
		}
	}
}