Exemple #1
0
  void ScalarFiniteElement<D> ::
  CalcDShape (const IntegrationPoint & ip, 
	      FlatMatrixFixWidth<D> dshape) const
  
  {
    static bool firsttime = true;
    if (firsttime)
      {
	cout << "WARNING: CalcDShape not overloaded for class, using numerical differentiation " << typeid(this).name() << ", ndof = " << ndof << endl;
        firsttime = false;
      }
    
    int nd = GetNDof();
    int sdim = D;

    double eps = 2e-5;
    ArrayMem<double, 100> hm1(nd), hm2(nd), hm3(nd), hm4(nd);
    FlatVector<> 
      shape1(nd, &hm1[0]), 
      shape2(nd, &hm2[0]), 
      shape3(nd, &hm3[0]), 
      shape4(nd, &hm4[0]);

    for (int i = 0; i < sdim; i++)
      {
	IntegrationPoint ip1 = ip;
	IntegrationPoint ip2 = ip;
        ip1(i) -= eps;
        ip2(i) += eps;
	CalcShape (ip1, shape1);
	CalcShape (ip2, shape2);

        ip1(i) -= eps;
        ip2(i) += eps;
	CalcShape (ip1, shape3);
	CalcShape (ip2, shape4);

	for (int j = 0; j < nd; j++)
	  dshape(j, i) = 
	    2/(3*eps) * (shape2(j) - shape1(j)) 
	    -1/(12*eps) * (shape4(j) - shape3(j));
      }
  }
Exemple #2
0
void			Resum::drawBorder(const sf::Vector2f &pos)
{
  sf::RectangleShape	shape(sf::Vector2f(1000, 500));
  sf::RectangleShape	shape2(sf::Vector2f(1000, 10));
  sf::RectangleShape	shape3(sf::Vector2f(1000, 3));
  
  shape.setPosition(pos.x - 500, pos.y + 300);
  shape2.setPosition(pos.x - 500, pos.y + 300);
  shape.setFillColor(sf::Color(20, 121, 17));
  shape2.setFillColor(sf::Color(8, 57, 6));
  _window->draw(shape);
  _window->draw(shape2);

  shape3.setFillColor(sf::Color(8, 57, 6));
  shape3.setPosition(pos.x - 500, pos.y + 360);
  _window->draw(shape3);
}
Exemple #3
0
void ThinPlateSplineShapeTransformerImpl::estimateTransformation(InputArray _pts1, InputArray _pts2,
                                                               std::vector<DMatch>& _matches )
{
    Mat pts1 = _pts1.getMat();
    Mat pts2 = _pts2.getMat();
    CV_Assert((pts1.channels()==2) && (pts1.cols>0) && (pts2.channels()==2) && (pts2.cols>0));
    CV_Assert(_matches.size()>1);

    if (pts1.type() != CV_32F)
        pts1.convertTo(pts1, CV_32F);
    if (pts2.type() != CV_32F)
        pts2.convertTo(pts2, CV_32F);

    // Use only valid matchings //
    std::vector<DMatch> matches;
    for (size_t i=0; i<_matches.size(); i++)
    {
        if (_matches[i].queryIdx<pts1.cols &&
            _matches[i].trainIdx<pts2.cols)
        {
            matches.push_back(_matches[i]);
        }
    }

    // Organizing the correspondent points in matrix style //
    Mat shape1((int)matches.size(),2,CV_32F); // transforming shape
    Mat shape2((int)matches.size(),2,CV_32F); // target shape
    for (int i=0, end = (int)matches.size(); i<end; i++)
    {
        Point2f pt1=pts1.at<Point2f>(0,matches[i].queryIdx);
        shape1.at<float>(i,0) = pt1.x;
        shape1.at<float>(i,1) = pt1.y;

        Point2f pt2=pts2.at<Point2f>(0,matches[i].trainIdx);
        shape2.at<float>(i,0) = pt2.x;
        shape2.at<float>(i,1) = pt2.y;
    }
    shape1.copyTo(shapeReference);

    // Building the matrices for solving the L*(w|a)=(v|0) problem with L={[K|P];[P'|0]}

    //Building K and P (Neede to buil L)
    Mat matK((int)matches.size(),(int)matches.size(),CV_32F);
    Mat matP((int)matches.size(),3,CV_32F);
    for (int i=0, end=(int)matches.size(); i<end; i++)
    {
        for (int j=0; j<end; j++)
        {
            if (i==j)
            {
                matK.at<float>(i,j)=float(regularizationParameter);
            }
            else
            {
                matK.at<float>(i,j) = distance(Point2f(shape1.at<float>(i,0),shape1.at<float>(i,1)),
                                               Point2f(shape1.at<float>(j,0),shape1.at<float>(j,1)));
            }
        }
        matP.at<float>(i,0) = 1;
        matP.at<float>(i,1) = shape1.at<float>(i,0);
        matP.at<float>(i,2) = shape1.at<float>(i,1);
    }

    //Building L
    Mat matL=Mat::zeros((int)matches.size()+3,(int)matches.size()+3,CV_32F);
    Mat matLroi(matL, Rect(0,0,(int)matches.size(),(int)matches.size())); //roi for K
    matK.copyTo(matLroi);
    matLroi = Mat(matL,Rect((int)matches.size(),0,3,(int)matches.size())); //roi for P
    matP.copyTo(matLroi);
    Mat matPt;
    transpose(matP,matPt);
    matLroi = Mat(matL,Rect(0,(int)matches.size(),(int)matches.size(),3)); //roi for P'
    matPt.copyTo(matLroi);

    //Building B (v|0)
    Mat matB = Mat::zeros((int)matches.size()+3,2,CV_32F);
    for (int i=0, end = (int)matches.size(); i<end; i++)
    {
        matB.at<float>(i,0) = shape2.at<float>(i,0); //x's
        matB.at<float>(i,1) = shape2.at<float>(i,1); //y's
    }

    //Obtaining transformation params (w|a)
    solve(matL, matB, tpsParameters, DECOMP_LU);
    //tpsParameters = matL.inv()*matB;

    //Setting transform Cost and Shape reference
    Mat w(tpsParameters, Rect(0,0,2,tpsParameters.rows-3));
    Mat Q=w.t()*matK*w;
    transformCost=fabs(Q.at<float>(0,0)*Q.at<float>(1,1));//fabs(mean(Q.diag(0))[0]);//std::max(Q.at<float>(0,0),Q.at<float>(1,1));
    tpsComputed=true;
}
Exemple #4
0
int			main(int ac, char **av) {
    SocketTCPClient	client;
    sf::RenderWindow	window(sf::VideoMode(800, 800), "SFML color game");
    sf::CircleShape	shape(400.f);
    sf::CircleShape	shape2(300.f);
    sf::CircleShape	shape3(200.f);
    sf::Color		color;
    sf::Color		color2;
    sf::Color		color3;
    FDSet		fdSet;
    char		buff[1024];
    std::string		entry;
    int			nbRead;
    sf::Event		event;
    struct timeval	tv;

    if (ac != 4)
    {
	std::cout << "./client host port name" << std::endl;
	return (0);
    }
    client.start();
    client.connectToServer(av[1], atoi(av[2]));
    color = sf::Color::Red;
    color2 = sf::Color::Yellow;
    color3 = sf::Color::Green;
    shape.setFillColor(color);
    shape2.setFillColor(color2);
    shape3.setFillColor(color3);
    while (window.isOpen())
    {
	fdSet.zero();
	fdSet.set(&client);
	fdSet.set(0);
	tv.tv_sec = 1;
	tv.tv_usec = 0;
	if (!Select::call(&fdSet, NULL, &tv))
	{
	    std::cout << "Client : Socket managment failed" << std::endl;
	    return (-1);
	}
	if (fdSet.isset(&client))
	{
	    memset(buff, 0, 1024);		
	    if ((nbRead = client.receive(buff, 1024)) == 0)
	    {
		client.close();
		std::cout << "Server left" << std::endl;
		return 0;
	    }
	    buff[nbRead] = 0;
	    if (std::string(buff) == "BIENVENU")
	    {
		client.send(("player|" + std::string(av[3])).c_str(), 7 + std::string(av[3]).length());
	    }
	    else if (std::string(buff) == "kick")
	    {
		std::cout << "You are kicked by server" << std::endl;
		return (0);
	    }
	    else if (std::string(buff) == "1")
	    {
		color = sf::Color::Blue;
		color2 = sf::Color::Red;
		color3 = sf::Color::Yellow;

		shape.setFillColor(color);
		shape2.setFillColor(color2);
		shape3.setFillColor(color3);

	    }
	    else if (std::string(buff) == "2")
	    {
		color = sf::Color::Black;
		color2 = sf::Color::White;
		color3 = sf::Color::Blue;

		shape.setFillColor(color);
		shape2.setFillColor(color2);
		shape3.setFillColor(color3);

	    }
	    else if (std::string(buff) == "3")
	    {
		color = sf::Color::Red;
		color2 = sf::Color::Yellow;
		color3 = sf::Color::Green;
		shape.setFillColor(color);
		shape2.setFillColor(color2);
		shape3.setFillColor(color3);

	    }
	    std::cout << "msg : " << buff << std::endl;
	}	
	if (fdSet.isset(0))
	{
	    std::getline(std::cin, entry);
	    client.send(entry.c_str(), entry.length());
	}	
	while (window.pollEvent(event))
	{
	    if (event.type == sf::Event::Closed)
		window.close();
	}
	window.clear();
	window.draw(shape);
	window.draw(shape2);
	window.draw(shape3);
	window.display();
    }
}
Exemple #5
0
void TestShapePainting::testPaintOrder()
{
    // the stacking order determines the painting order so things on top
    // get their paint called last.
    // Each shape has a zIndex and within the children a container has
    // it determines the stacking order. Its important to realize that
    // the zIndex is thus local to a container, if you have layer1 and layer2
    // with both various child shapes the stacking order of the layer shapes
    // is most important, then within this the child shape index is used.

    class OrderedMockShape : public MockShape {
    public:
        OrderedMockShape(QList<MockShape*> &list) : order(list) {}
        void paint(QPainter &painter, const KViewConverter &converter) {
            order.append(this);
            MockShape::paint(painter, converter);
        }
        QList<MockShape*> &order;
    };

    QList<MockShape*> order;

    MockContainer top;
    top.setZIndex(2);
    OrderedMockShape shape1(order);
    shape1.setZIndex(5);
    OrderedMockShape shape2(order);
    shape2.setZIndex(0);
    top.addShape(&shape1);
    top.addShape(&shape2);

    MockContainer bottom;
    bottom.setZIndex(1);
    OrderedMockShape shape3(order);
    shape3.setZIndex(-1);
    OrderedMockShape shape4(order);
    shape4.setZIndex(9);
    bottom.addShape(&shape3);
    bottom.addShape(&shape4);

    MockCanvas canvas;
    KShapeManager manager(&canvas);
    manager.addShape(&top);
    manager.addShape(&bottom);
    QCOMPARE(manager.shapes().count(), 6);

    QImage image(100, 100,  QImage::Format_Mono);
    QPainter painter(&image);
    KViewConverter vc;
    manager.paint(painter, vc, false);
    QCOMPARE(top.paintedCount, 1);
    QCOMPARE(bottom.paintedCount, 1);
    QCOMPARE(shape1.paintedCount, 1);
    QCOMPARE(shape2.paintedCount, 1);
    QCOMPARE(shape3.paintedCount, 1);
    QCOMPARE(shape4.paintedCount, 1);

    QCOMPARE(order.count(), 4);
    QVERIFY(order[0] == &shape3); // lowest first
    QVERIFY(order[1] == &shape4);
    QVERIFY(order[2] == &shape2);
    QVERIFY(order[3] == &shape1);

    // again, with clipping.
    order.clear();
    painter.setClipRect(0, 0, 100, 100);
    manager.paint(painter, vc, false);
    QCOMPARE(top.paintedCount, 2);
    QCOMPARE(bottom.paintedCount, 2);
    QCOMPARE(shape1.paintedCount, 2);
    QCOMPARE(shape2.paintedCount, 2);
    QCOMPARE(shape3.paintedCount, 2);
    QCOMPARE(shape4.paintedCount, 2);

    QCOMPARE(order.count(), 4);
    QVERIFY(order[0] == &shape3); // lowest first
    QVERIFY(order[1] == &shape4);
    QVERIFY(order[2] == &shape2);
    QVERIFY(order[3] == &shape1);

    order.clear();
    
    MockContainer root;
    root.setZIndex(0);
    
    MockContainer branch1;
    branch1.setZIndex(1);
    OrderedMockShape child1_1(order);
    child1_1.setZIndex(1);
    OrderedMockShape child1_2(order);
    child1_2.setZIndex(2);
    branch1.addShape(&child1_1);
    branch1.addShape(&child1_2);
    
    MockContainer branch2;
    branch2.setZIndex(2);
    OrderedMockShape child2_1(order);
    child2_1.setZIndex(1);
    OrderedMockShape child2_2(order);
    child2_2.setZIndex(2);
    branch2.addShape(&child2_1);
    branch2.addShape(&child2_2);
 
    root.addShape(&branch1);
    root.addShape(&branch2);
    
    QList<KShape*> sortedShapes;
    sortedShapes.append(&root);
    sortedShapes.append(&branch1);
    sortedShapes.append(&branch2);
    sortedShapes.append(branch1.shapes());
    sortedShapes.append(branch2.shapes());
    
    qSort(sortedShapes.begin(), sortedShapes.end(), KShape::compareShapeZIndex);
    QCOMPARE(sortedShapes.count(), 7);
    QVERIFY(sortedShapes[0] == &root);
    QVERIFY(sortedShapes[1] == &branch1);
    QVERIFY(sortedShapes[2] == &child1_1);
    QVERIFY(sortedShapes[3] == &child1_2);
    QVERIFY(sortedShapes[4] == &branch2);
    QVERIFY(sortedShapes[5] == &child2_1);
    QVERIFY(sortedShapes[6] == &child2_2);
}
Exemple #6
0
TEST(SAVE, SaveGraph)
{
	std::string expect_pbfile = testdir + "/graph.pb";
	std::string got_pbfile = "got_graph.pb";
	cortenn::Graph graph;
	std::vector<ade::TensptrT> roots;

	pbm::PathedMapT labels;
	// subtree one
	ade::Shape shape({3, 7});
	ade::TensptrT osrc(new MockTensor(shape));

	ade::Shape shape2({7, 3});
	ade::TensptrT osrc2(new MockTensor(shape2));

	labels[osrc] = {"global", "osrc"};
	labels[osrc2] = {"global", "osrc2"};

	{
		ade::TensptrT src(new MockTensor(shape));

		ade::Shape shape3({3, 1, 7});
		ade::TensptrT src2(new MockTensor(shape3));

		ade::TensptrT dest(ade::Functor::get(ade::Opcode{"-", 0}, {
			{src2, ade::identity},
			{ade::TensptrT(ade::Functor::get(ade::Opcode{"@", 1}, {
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"/", 2}, {
					{ade::TensptrT(ade::Functor::get(ade::Opcode{"neg", 3}, {
						{osrc, ade::identity},
					})), ade::identity},
					{ade::TensptrT(ade::Functor::get(ade::Opcode{"+", 4}, {
						{ade::TensptrT(
							ade::Functor::get(ade::Opcode{"sin", 5}, {
							{src, ade::identity}})), ade::identity},
						{src, ade::identity},
					})), ade::identity}
				})), ade::permute({1, 0})},
				{osrc2, ade::identity}
			})), ade::permute({1, 2, 0})},
		}));
		roots.push_back(dest);

		labels[src] = {"subtree", "src"};
		labels[src2] = {"subtree", "src2"};
		labels[dest] = {"subtree", "dest"};
	}

	// subtree two
	{
		ade::Shape mshape({3, 3});
		ade::TensptrT src(new MockTensor(mshape));

		ade::TensptrT src2(new MockTensor(mshape));

		ade::TensptrT src3(new MockTensor(mshape));

		ade::TensptrT dest(ade::Functor::get(ade::Opcode{"-", 0}, {
			{src, ade::identity},
			{ade::TensptrT(ade::Functor::get(ade::Opcode{"*", 6}, {
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"abs", 7}, {
					{src, ade::identity},
				})), ade::identity},
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"exp", 8}, {
					{src2, ade::identity},
				})), ade::identity},
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"neg", 3}, {
					{src3, ade::identity},
				})), ade::identity},
			})), ade::identity},
		}));
		roots.push_back(dest);

		labels[src] = {"subtree2", "src"};
		labels[src2] = {"subtree2", "src2"};
		labels[src3] = {"subtree2", "src3"};
		labels[dest] = {"subtree2", "dest"};
	}

	pbm::GraphSaver<TestSaver> saver;
	for (auto& root : roots)
	{
		root->accept(saver);
	}

	saver.save(graph, labels);

	{
		std::fstream gotstr(got_pbfile,
			std::ios::out | std::ios::trunc | std::ios::binary);
		ASSERT_TRUE(gotstr.is_open());
		ASSERT_TRUE(graph.SerializeToOstream(&gotstr));
	}

	std::fstream expect_ifs(expect_pbfile, std::ios::in | std::ios::binary);
	std::fstream got_ifs(got_pbfile, std::ios::in | std::ios::binary);
	ASSERT_TRUE(expect_ifs.is_open());
	ASSERT_TRUE(got_ifs.is_open());

	std::string expect;
	std::string got;
	// skip the first line (it contains timestamp)
	expect_ifs >> expect;
	got_ifs >> got;
	for (size_t lineno = 1; expect_ifs && got_ifs; ++lineno)
	{
		expect_ifs >> expect;
		got_ifs >> got;
		EXPECT_STREQ(expect.c_str(), got.c_str()) << "line number " << lineno;
	}
}
Exemple #7
0
static void TraceShape(  // write an image file showing current shape on the image
    const Shape& shape,  // in: current search shape
    const Image& pyrimg, // in: image scaled to this pyramid level
    int          ilev,   // in: pyramid level (0 is full size)
    int          iter,   // in: model iteration (-1 if start shape)
    const char*  suffix) // in
{
#if TRACE_IMAGES // will be 0 unless debugging (defined in stasm.h)

    static int index; // number images so they appear in order in the directory
    // start at index 30 because lower indices have already used for facedet etc.
    if (strcmp(suffix, "start") == 0)
        index = 30;
    Image img; // pyrimg rescaled back to full size image (after rescaling by eyemouth)
    const double RESOLUTION = 2; // 1 for no extra resolution, 2 for double resolution
    const double rescale = RESOLUTION / GetPyrScale(ilev);
    cv::resize(pyrimg, img, cv::Size(), rescale, rescale, cv::INTER_NEAREST);
    CImage cimg; cvtColor(img, cimg, CV_GRAY2BGR); // color image
    DesaturateImg(cimg);
    Shape shape1(RoundMat(shape));
    shape1 += .4; // put shape points in center of rescaled pixels
    shape1 *= rescale;
    DrawShape(cimg, shape1, C_YELLOW, false, 1);
    char path[SLEN];
    if (iter < 0) // start shape?
        sprintf(path, "%s_%2.2d_%s.bmp", Base(imgpath_g), index, suffix);
    else
        sprintf(path, "%s_%2.2d_lev%d_iter%d_%s.bmp",
                Base(imgpath_g), index, ilev, iter, suffix);
    ImgPrintf(cimg, 10 * RESOLUTION, 20 * RESOLUTION, C_YELLOW, 2, path);
    if (iter >= 0)
    {
        // draw 1D patch boundary at one point (patch is drawn
        // horizontal, not rotated to shape boundary as it should be)
        // [Thanks to Satish Lokkoju for RoundMat fix]

        Shape shape2(RoundMat(shape));
        int ipoint = 0;
        int proflen = 9; // TASM_1D_PROFLEN
        int x1 = cvRound(shape2(ipoint, IX)) - proflen / 2;
        int x2 = x1 + proflen;
        int y1 = cvRound(shape2(ipoint, IY));
        int y2 = y1 + 1;
        rectangle(cimg,
                  cv::Point(cvRound(rescale * x1), cvRound(rescale * y1)),
                  cv::Point(cvRound(rescale * x2), cvRound(rescale * y2)),
                  CV_RGB(255,0,0), 1);

        // draw 2D patch boundary at one point

        if (ilev <= HAT_START_LEV) // we use HATs only at upper pyr levs
        {
            // get position of left eye pupil by first converting to a shape17
            ipoint = 0; // assume we can't get position of left eye pupil
            Shape newshape(Shape17OrEmpty(shape2));
            if (newshape.rows) // successfully converted to a shape17?
                ipoint = L17_LPupil;
            else
                newshape = shape2;
            #define round2(x) 2 * cvRound((x) / 2)
            int patchwidth = HAT_PATCH_WIDTH + round2(ilev * HAT_PATCH_WIDTH_ADJ);
            x1 = cvRound(newshape(ipoint, IX)) - patchwidth / 2;
            x2 = x1 + patchwidth;
            y1 = cvRound(newshape(ipoint, IY)) - patchwidth / 2;
            y2 = y1 + patchwidth;
            rectangle(cimg,
                      cv::Point(cvRound(rescale * x1), cvRound(rescale * y1)),
                      cv::Point(cvRound(rescale * x2), cvRound(rescale * y2)),
                      CV_RGB(255,0,0), 1);
        }
    }
    lprintf("%s\n", path);
    if (!cv::imwrite(path, cimg))
        Err("Cannot write %s", path);
    index++;

#endif // TRACE_IMAGES
}