bool KoConnectionShapePrivate::intersects(const QPointF &p1, const QPointF &d1, const QPointF &p2, const QPointF &d2, QPointF &isect)
{
    qreal sp1 = scalarProd(d1, p2 - p1);
    if (sp1 < 0.0)
        return false;

    qreal sp2 = scalarProd(d2, p1 - p2);
    if (sp2 < 0.0)
        return false;

    // use cross product to check if rays intersects at all
    qreal cp = crossProd(d1, d2);
    if (cp == 0.0) {
        // rays are parallel or coincidient
        if (p1.x() == p2.x() && d1.x() == 0.0 && d1.y() != d2.y()) {
            // vertical, coincident
            isect = 0.5 * (p1 + p2);
        } else if (p1.y() == p2.y() && d1.y() == 0.0 && d1.x() != d2.x()) {
            // horizontal, coincident
            isect = 0.5 * (p1 + p2);
        } else {
            return false;
        }
    } else {
        // they are intersecting normally
        isect = p1 + sp1 * d1;
    }

    return true;
}
int main(int argc, char** argv) {
    if(argc != 3) {
        printf("Usage: ./gram_schmidt vector_length number_of_vectors\n");
        exit(EXIT_FAILURE);
    }

    double** v;
    double **q;
    double temp_norm, sigma;

    int row = atoi(argv[1]);
    //int col = row;
    int col = atoi(argv[2]);

    if(row < col) {
        printf("It is not possible to create an orthogonal base with such values. \n");
        exit(-1);
    }

    int k,i,j,ttime;

    v = alloc_matrix(row, col);
    q = alloc_matrix(row, col);

    // initializes v with random values
    srand(time(NULL));
    init(v, row, col);

    // compute
    ttime=timer();


    for(i=0; i<row; i++) {
        temp_norm = vecNorm(v[i],col);
        for (k=0; k<col; k++)
            q[i][k] = v[i][k]/temp_norm;
        #pragma omp parallel for private(temp_norm, k, j, sigma) schedule(dynamic)
        for(j=i+1; j<row; j++) {
            sigma = scalarProd(q[i], v[j], col);
            for(k=0; k<col; k++)
                v[j][k] -=sigma*q[i][k];
        }
    }




    ttime=timer()-ttime;
    printf("Time: %f \n",ttime/1000000.0);
    printf("Check orthogonality: %e \n",scalarProd(q[col/2],  q[col/3], row));

}
QPointF KoConnectionShapePrivate::perpendicularDirection(const QPointF &p1, const QPointF &d1, const QPointF &p2)
{
    QPointF perpendicular(d1.y(), -d1.x());
    qreal sp = scalarProd(perpendicular, p2 - p1);
    if (sp < 0.0)
        perpendicular *= -1.0;

    return perpendicular;
}
void KoConnectionShapePrivate::normalPath(const qreal MinimumEscapeLength)
{
    // Clear the path to build it again.
    path.clear();
    path.append(handles[KoConnectionShape::StartHandle]);

    QList<QPointF> edges1;
    QList<QPointF> edges2;

    QPointF direction1 = escapeDirection(KoConnectionShape::StartHandle);
    QPointF direction2 = escapeDirection(KoConnectionShape::EndHandle);

    QPointF edgePoint1 = handles[KoConnectionShape::StartHandle] + MinimumEscapeLength * direction1;
    QPointF edgePoint2 = handles[KoConnectionShape::EndHandle] + MinimumEscapeLength * direction2;

    edges1.append(edgePoint1);
    edges2.prepend(edgePoint2);

    if (handleConnected(KoConnectionShape::StartHandle) && handleConnected(KoConnectionShape::EndHandle)) {
        QPointF intersection;
        bool connected = false;
        do {
            // first check if directions from current edge points intersect
            if (intersects(edgePoint1, direction1, edgePoint2, direction2, intersection)) {
                // directions intersect, we have another edge point and be done
                edges1.append(intersection);
                break;
            }

            // check if we are going toward the other handle
            qreal sp = scalarProd(direction1, edgePoint2 - edgePoint1);
            if (sp >= 0.0) {
                // if we are having the same direction, go all the way toward
                // the other handle, else only go half the way
                if (direction1 == direction2)
                    edgePoint1 += sp * direction1;
                else
                    edgePoint1 += 0.5 * sp * direction1;
                edges1.append(edgePoint1);
                // switch direction
                direction1 = perpendicularDirection(edgePoint1, direction1, edgePoint2);
            } else {
                // we are not going into the same direction, so switch direction
                direction1 = perpendicularDirection(edgePoint1, direction1, edgePoint2);
            }
        } while (! connected);
    }

    path.append(edges1);
    path.append(edges2);

    path.append(handles[KoConnectionShape::EndHandle]);
}
示例#5
0
文件: cube.cpp 项目: thnake/collision
void inline collSpherePlanes()
{
	// p= startpunkt 
	// q=endpunkt
	// a,b,c = Dreieckspunkte
	// u,v,w baryzentrische koordinaten zum Schnittpunkt s
	// s = u*a + v*b + w*c
	triangle tri;
	bowl *b;
	float u,v,w;
	int check = 0;
	Vector s(3); // schnittpunkt mit dreieck
	float pace = 0;
	float lambda = 0;
	for(int h = 0; h < bcount; h++)
	{
		b = &bowls[h];
		
		if(b->fixed)
		{
			continue;
		}

		for(int i = 0; i < objects.size(); i++)
		{
			for(int j = 0; j < objects[i].triangles.size(); j++)
			{
				
				Vector dir(3);
				dir[0] = b->pace[0];
				dir[1] = b->pace[1];
				dir[2] = b->pace[2];
				normalizeVector(&dir);
				float len = getVectorLen(dir);
					tri = objects[i].triangles[j];
					// tri um länge des radius heranholen
					float sign = (scalarProd(&dir, &tri.normal) < 0) ? 1 : -1;
					
					tri.a[0] += b->radius*tri.normal[0]*sign;
					tri.a[1] += b->radius*tri.normal[1]*sign;
					tri.a[2] += b->radius*tri.normal[2]*sign;

					tri.b[0] += b->radius*tri.normal[0]*sign;
					tri.b[1] += b->radius*tri.normal[1]*sign;
					tri.b[2] += b->radius*tri.normal[2]*sign;

					tri.c[0] += b->radius*tri.normal[0]*sign;
					tri.c[1] += b->radius*tri.normal[1]*sign;
					tri.c[2] += b->radius*tri.normal[2]*sign;

					// broad phase:
					pace = getVectorLen(b->pace);

					
					// naiver test mit unendlicher  ebene
					check = checkIntersectRayPlane(&tri, &b->oldPos, &b->pace, &lambda);
					
					if (check == 0 || lambda > pace)
					{
						continue;
					}

					std::cout << lambda << "\r\n"; 

					//check = IntersectLineTriangle(b->oldPos, b->pos, tri.a, tri.b, tri.c, u,v,w, &tri);
					s[0] = b->oldPos[0] + lambda*dir[0];
					s[1] = b->oldPos[1] + lambda*dir[1];
					s[2] = b->oldPos[2] + lambda*dir[2];
					check = checkPointInTriangle(s, &tri);
					

 					Vector face = s - b->oldPos;
					normalizeVector(&face);
					float dot = scalarProd(&face, &dir);

					if(check == 1 && dot > 0)
					{
						// schauen, ob schnittpunkt überschritten wurde
						if(lambda <= pace)
						{
							Vector m(3);
							m[0] = b->oldPos[0] + b->pace[0]/2;
							m[1] = b->oldPos[1] + b->pace[1]/2;
							m[2] = b->oldPos[2] + b->pace[2]/2;

							// schauen, ob schnittpunkt im radius ist
							float d = getDistance(s,m); 
							if(d < pace)
							{
								// kollision
								Vector out(3);
								float subLen = abs(getDistance(s, b->pos));
								getOutVector(dir, tri.normal, &out);

						

								b->pos[0] = s[0] + subLen * out[0]; // + out[0] * subLen;
								b->pos[1] = s[1] + subLen * out[1]; // + out[1] * subLen; 
								b->pos[2] = s[2] + subLen * out[2]; // + out[2] * subLen; 

								// neue schrittweite 
								// gleitanteil
								Vector slide = out - tri.normal;
								float outDotN = abs(scalarProd(&out,&tri.normal));
								
								slide[0]= (out[0] - outDotN*tri.normal[0]);
								slide[1]= (out[1] - outDotN*tri.normal[1]);
								slide[2]= (out[2] - outDotN*tri.normal[2]);
								
								out[0] = tri.normal[0] + 8*slide[0];
								out[1] = tri.normal[1] + 8*slide[1];
								out[2] = tri.normal[2] + 8*slide[2];
								normalizeVector(&out);

								
								// kugel fällt durch das mesh durch, wenn aktiviert
								
								if(friction)
								{
									pace *= b->friction;
								}
								if(pace <= 0.001 && fix)
								{
									pace = 0;
									b->fixed = true;
								}

								b->pace[0] = out[0] * pace;
								b->pace[1] = out[1] * pace;
								b->pace[2] = out[2] * pace;

							}
						}
					
					}
			}
		}
	}
}
//derivative of activation function 1/(cosh(x)^2)
double fdx(Neuron* neuron, double* in) {
	return 1 / pow(cosh(scalarProd(neuron->w, in, N)), 2);
}
//activation function
double f(Neuron* neuron, double* in) {
	return tanh(scalarProd(neuron->w, in, N));
}