klayout/src/unit_tests/dbPath.cc

365 lines
37 KiB
C++
Raw Normal View History

/*
KLayout Layout Viewer
2017-02-12 15:28:14 +01:00
Copyright (C) 2006-2017 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dbPath.h"
#include "tlVector.h"
#include "tlReuseVector.h"
#include "utHead.h"
#include <vector>
std::string to_string (const tl::vector<db::Point> &pts)
{
std::string s = "(";
// the hull contour
for (tl::vector<db::Point>::const_iterator p = pts.begin (); p != pts.end (); ++p) {
if (p != pts.begin ()) {
s += ";";
}
s += p->to_string ();
}
s += ")";
return s;
}
TEST(1)
{
db::Path p;
db::Path empty;
db::Box b;
EXPECT_EQ (empty == p, true);
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
p.assign (c1.begin (), c1.end ());
p.width (200);
b = p.box ();
// simple: EXPECT_EQ (b, db::Box (-200, -200, 200, 1200));
EXPECT_EQ (b, db::Box (-100, 0, 100, 1000));
EXPECT_EQ (p.to_string (), "(0,0;0,1000) w=200 bx=0 ex=0 r=false");
db::DPath dp (p, db::cast_op<db::DPoint, db::Point> (), db::cast_op<db::Coord, double> ());
EXPECT_EQ (dp.to_string (), "(0,0;0,1000) w=200 bx=0 ex=0 r=false");
db::Path ip = db::Path (dp);
EXPECT_EQ (ip.to_string (), "(0,0;0,1000) w=200 bx=0 ex=0 r=false");
db::Path::pointlist_type pts;
p.hull (pts);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1000;100,1000;100,0)");
// asymmetric width
pts.clear ();
p.hull (pts, 50, 150);
EXPECT_EQ (to_string (pts), "(-50,0;-50,1000;150,1000;150,0)");
pts.clear ();
p.hull (pts, 50, 0);
EXPECT_EQ (to_string (pts), "(-50,0;-50,1000;0,1000;0,0)");
pts.clear ();
p.hull (pts, -50, 150);
EXPECT_EQ (to_string (pts), "(50,0;50,1000;150,1000;150,0)");
}
TEST(2)
{
db::Path p;
db::Box b;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (10, 1000));
p.assign (c1.begin (), c1.end ());
p.width (200);
b = p.box ();
EXPECT_EQ (b, db::Box (-100, 0, 100, 1100));
EXPECT_EQ (p.to_string (), "(0,0;0,1000;10,1000) w=200 bx=0 ex=0 r=false");
db::Path::pointlist_type pts;
p.hull (pts);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1100;10,1100;10,900;100,900;100,0)");
}
TEST(2a)
{
db::Path p;
db::Box b;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (1000, 1000));
p.assign (c1.begin (), c1.end ());
p.width (200);
db::Path::pointlist_type pts;
p.hull (pts, 100, -50);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1100;1000,1100;1000,1050;-50,1050;-50,0)");
pts.clear ();
p.hull (pts, 100, 50);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1100;1000,1100;1000,950;50,950;50,0)");
pts.clear ();
p.hull (pts, -50, 100);
EXPECT_EQ (to_string (pts), "(50,0;50,950;1000,950;1000,900;100,900;100,0)");
}
TEST(3)
{
db::Path p;
db::Box b;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (1000, 0));
p.assign (c1.begin (), c1.end ());
p.width (200);
b = p.box ();
// simple: EXPECT_EQ (b, db::Box (-200, -200, 1200, 1200));
EXPECT_EQ (b, db::Box (-100, -71, 1071, 1142));
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,0) w=200 bx=0 ex=0 r=false");
db::Path::pointlist_type pts;
p.hull (pts);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1100;0,1142;1071,71;929,-71;100,758;100,0)");
}
TEST(4)
{
db::Path p;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (1000, 2000));
p.assign (c1.begin (), c1.end ());
p.width (200);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=200 bx=0 ex=0 r=false");
db::Path::pointlist_type pts;
p.hull (pts);
EXPECT_EQ (to_string (pts), "(-100,0;-100,1042;929,2071;1071,1929;100,958;100,0)");
db::Box bx;
for (db::Path::pointlist_type::const_iterator pt = pts.begin (); pt != pts.end (); ++pt) {
bx += *pt;
}
EXPECT_EQ (p.box (), bx);
}
TEST(5)
{
db::Path p;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (1000, 2000));
p.assign (c1.begin (), c1.end ());
p.extensions (50, 150);
p.width (200);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=200 bx=50 ex=150 r=false");
db::Path::pointlist_type pts;
p.hull (pts);
EXPECT_EQ (to_string (pts), "(-100,-50;-100,1042;1035,2177;1177,2035;100,958;100,-50)");
db::Box bx;
for (db::Path::pointlist_type::const_iterator pt = pts.begin (); pt != pts.end (); ++pt) {
bx += *pt;
}
EXPECT_EQ (p.box (), bx);
}
TEST(6)
{
db::Path p;
std::vector <db::Point> c1;
c1.push_back (db::Point (0, 0));
c1.push_back (db::Point (0, 1000));
c1.push_back (db::Point (1000, 2000));
p.assign (c1.begin (), c1.end ());
p.extensions (50, 150);
p.width (200);
p.round (true);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=200 bx=50 ex=150 r=true");
p.width (150);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=150 bx=50 ex=150 r=true");
p.round(false);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=150 bx=50 ex=150 r=false");
p.width (200);
p.round (true);
EXPECT_EQ (p.to_string (), "(0,0;0,1000;1000,2000) w=200 bx=50 ex=150 r=true");
db::Path::pointlist_type pts;
p.hull (pts, 4);
EXPECT_EQ (to_string (pts), "(-41,-50;-100,-21;-100,1042;973,2115;1077,2135;1135,2077;1115,1973;100,958;100,-21;41,-50)");
pts.clear ();
p.hull (pts, 2);
db::Box bx;
for (db::Path::pointlist_type::const_iterator pt = pts.begin (); pt != pts.end (); ++pt) {
bx += *pt;
}
EXPECT_EQ (p.box (), bx);
}
TEST(7)
{
db::DPath path;
std::string s ("(0,0;0,1000;1000,2000) w=200 bx=50 ex=150 r=true");
tl::Extractor ex (s.c_str ());
ex.read (path);
tl::reuse_vector<db::DPath> v;
for (int i = 0; i < 10; ++i) {
v.insert (path);
}
EXPECT_EQ (v.begin ()->to_string (), s);
for (int i = 0; i < 9; ++i) {
v.erase (v.begin ());
}
EXPECT_EQ (v.begin ()->to_string (), s);
v.clear ();
}
TEST(8a)
{
db::DPath path;
std::string s ("(0,0;0,1000;1000,2000) w=200 bx=50 ex=150 r=true");
tl::Extractor ex (s.c_str ());
ex.read (path);
// 434400 = (1000+1000*sqrt(2))*200+200*200*(4.0-M_PI)
EXPECT_EQ (tl::to_string (path.area ()), "517179.006331");
EXPECT_EQ (tl::to_string (path.perimeter ()), "5477.2678875");
EXPECT_EQ (tl::to_string (path.length ()), "2614.21356237");
}
TEST(8b)
{
db::DPath path;
std::string s ("(0,0;0,1000;1000,2000) w=200 bx=50 ex=150");
tl::Extractor ex (s.c_str ());
ex.read (path);
EXPECT_EQ (tl::to_string (path.area ()), "522842.712475");
EXPECT_EQ (tl::to_string (path.perimeter ()), "5628.42712475");
EXPECT_EQ (tl::to_string (path.length ()), "2614.21356237");
}
TEST(9)
{
db::Path path;
{
std::string s ("(0,0;1100,0;2200,0;3300,0;4400,0;5500,0;6600,0;7700,0;8800,1;9900,1;11000,1;12100,2;13200,2;14300,3;15400,3;16500,4;17600,5;18700,6;19800,7;20900,8;22000,9;23100,11;24200,13;25300,14;26400,16;27500,19;28600,21;29700,23;30800,26;31900,29;33000,32;34100,35;35200,39;36300,43;37400,47;38500,51;39600,55;40700,60;41800,65;42900,70;44000,76;45100,81;46200,88;47300,94;48400,101;49500,108;50600,115;51700,123;52800,131;53900,139;55000,148;56100,157;57200,166;58300,176;59400,186;60500,196;61600,207;62700,218;63800,230;64900,242;66000,254;67100,267;68200,281;69300,294;70400,308;71500,323;72600,338;73700,354;74800,370;75900,386;77000,403;78100,420;79200,438;80300,457;81400,475;82500,495;83600,515;84700,535;85800,556;86900,578;88000,600;89100,622;90200,645;91300,669;92400,693;93500,718;94600,743;95700,769;96800,796;97900,823;99000,851;100100,879;101200,908;102300,938;103400,968;104500,999;105600,1030;106700,1063;107800,1095;108900,1129;110000,1163;111100,1198;112200,1233;113300,1269;114400,1306;115500,1343;116600,1382;117700,1420;118800,1460;119900,1500;121000,1541;122100,1583;123200,1625;124300,1669;125400,1713;126500,1757;127600,1803;128700,1849;129800,1896;130900,1943;132000,1992;133100,2041;134200,2091;135300,2142;136400,2194;137500,2246;138600,2299;139700,2353;140800,2408;141900,2464;143000,2520;144100,2577;145200,2635;146300,2694;147400,2754;148500,2815;149600,2876;150700,2939;151800,3002;152900,3066;154000,3131;155100,3197;156200,3263;157300,3331;158400,3399;159500,3469;160600,3539;161700,3610;162800,3682;163900,3755;165000,3829;166100,3903;167200,3979;168300,4056;169400,4133;170500,4212;171600,4291;172700,4372;173800,4453;174900,4535;176000,4618;177100,4702;178200,4787;179300,4874;180400,4961;181500,5049;182600,5138;183700,5228;184800,5318;185900,5410;187000,5503;188100,5597;189200,5692;190300,5788;191400,5885;192500,5983;193600,6082;194700,6182;195800,6283;196900,6385;198000,6488;199100,6592;200200,6697;201300,6803;202400,6910;203500,7018;204600,7127;205700,7237;206800,7349;207900,7461;209000,7574;210100,7689;211200,7804;212300,7921;213400,8038;214500,8157;215600,8277;216700,8398;217800,8519;218900,8642;220000,8766;221100,8892;222200,9018;223300,9145;224400,9273;225500,9403;226600,9533;227700,9665;228800,9797;229900,9931;231000,10066;232100,10202;233200,10339;234300,10477;235400,10617;236500,10757;237600,10899;238700,11041;239800,11185;240900,11330;242000,11476;243100,11623;244200,11771;245300,11920;246400,12070;247500,12222;248600,12374;249700,12528;250800,12683;251900,12839;253000,12996;254100,13154;255200,13314;256300,13474;257400,13636;258500,13798;259600,13962;260700,14127;261800,14293;262900,14461;264000,14629;265100,14799;266200,14969;267300,15141;268400,15314;269500,15488;270600,15663;271700,15839;272800,16017;273900,16195;275000,16375;276100,16556;277200,16738;278300,16921;279400,17105;280500,17290;281600,17477;282700,17664;283800,17853;284900,18043;286000,18234;287100,18426;288200,18619;289300,18814;290400,19009;291500,19206;292600,19404;293700,19603;294800,19803;295900,20004;297000,20206;298100,20410;299200,20614;300300,20820;301400,21026;302500,21234;303600,21443;304700,21654;305800,21865;306900,22077;308000,22291;309100,22505;310200,22721;311300,22938;312400,23156;313500,23375;314600,23595;315700,23816;316800,24038;317900,24262;319000,24486;320100,24712;321200,24939;322300,25166;323400,25395;324500,25625;325600,25856;326700,26089;327800,26322;328900,26556;330000,26792;331100,27028;332200,27266;333300,27504;334400,27744;335500,27985;336600,28227;337700,28469;338800,28713;339900,28958;341000,29205;342100,29452;343200,29700;344300,29949;345400,30199;346500,30451;347600,30703;348700,30956;349800,31211;350900,31466;352000,31723;353100,31980;354200,32239;355300,32498;356400,32759;357500,33021;358600,33283;359700,33547;360800,33811;361900,34077;363000,34343;364100,34611;365200,34880;366300,35149;367400,35420;368500,35691;369600,35964;370700,36237;371800,36512;372900,36787;374000,37063;375100,37341;376200,37619;377300,37898;378400,38178;379500,38459;380600,38741;381700,39024;382800,39308;383900,39593
tl::Extractor ex (s.c_str ());
ex.read (path);
}
db::CplxTrans trans;
{
std::string s ("r0 *0.0436844933 -27409.4906073,-4721.49865694");
tl::Extractor ex (s.c_str ());
ex.read (trans);
}
std::string sref = "-27409,-4394,-27073,-4394,-27025,-4394,-26929,-4394,-26881,-4394,-26833,-4394,-26785,-4394,-26737,-4394,-26449,-4393,-26353,-4393,-26305,-4393,-26257,-4393,-26209,-4393,-26113,-4393,-25921,-4392,-25681,-4391,-25537,-4391,-25489,-4391,-25441,-4390,-25393,-4390,-25345,-4390,-25201,-4389,-25057,-4388,-24914,-4387,-24770,-4385,-24674,-4384,-24530,-4383,-24482,-4382,-24434,-4382,-24386,-4381,-24338,-4380,-24243,-4379,-24099,-4377,-24003,-4376,-23955,-4375,-23907,-4374,-23859,-4373,-23716,-4371,-23668,-4370,-23524,-4367,-23476,-4366,-23380,-4364,-23285,-4361,-23237,-4360,-23141,-4358,-23045,-4356,-22997,-4354,-22902,-4352,-22806,-4349,-22758,-4348,-22710,-4346,-22614,-4343,-22519,-4340,-22471,-4339,-22375,-4335,-22327,-4334,-22279,-4332,-22184,-4329,-22136,-4327,-22040,-4323,-21897,-4317,-21801,-4313,-21705,-4309,-21610,-4305,-21562,-4303,-21514,-4301,-21418,-4296,-21371,-4294,-21323,-4291,-21275,-4289,-21179,-4284,-21132,-4282,-21084,-4279,-21036,-4277,-20988,-4274,-20893,-4269,-20797,-4263,-20749,-4260,-20702,-4258,-20606,-4252,-20510,-4246,-20415,-4240,-20367,-4237,-20319,-4234,-20271,-4231,-20176,-4224,-20128,-4221,-20033,-4214,-19937,-4207,-19841,-4200,-19794,-4197,-19746,-4193,-19698,-4189,-19650,-4186,-19555,-4178,-19507,-4174,-19459,-4170,-19364,-4163,-19316,-4159,-19268,-4155,-19220,-4151,-19173,-4146,-19125,-4142,-19077,-4138,-19029,-4134,-18982,-4130,-18934,-4125,-18886,-4121,-18838,-4116,-18791,-4112,-18743,-4107,-18695,-4103,-18647,-4098,-18600,-4094,-18552,-4089,-18504,-4084,-18456,-4079,-18361,-4070,-18313,-4065,-18218,-4055,-18122,-4045,-18075,-4039,-18027,-4034,-17931,-4024,-17884,-4018,-17836,-4013,-17740,-4002,-17692,-3997,-17645,-3991,-17549,-3980,-17454,-3968,-17406,-3962,-17358,-3957,-17311,-3951,-17263,-3945,-17215,-3939,-17120,-3927,-17024,-3914,-16977,-3908,-16929,-3902,-16881,-3895,-16833,-3889,-16786,-3883,-16738,-3876,-16690,-3870,-16595,-3856,-16547,-3850,-16499,-3843,-16452,-3836,-16404,-3829,-16356,-3823,-16261,-3809,-16165,-3795,-16117,-3788,-16070,-3780,-16022,-3773,-15927,-3759,-15831,-3744,-15783,-3736,-15736,-3729,-15688,-3721,-15640,-3714,-15592,-3706,-15497,-3691,-15449,-3683,-15402,-3675,-15354,-3667,-15306,-3659,-15258,-3651,-15211,-3643,-15115,-3627,-15067,-3619,-15020,-3610,-14972,-3602,-14924,-3594,-14877,-3585,-14781,-3569,-14733,-3560,-14686,-3551,-14638,-3543,-14590,-3534,-14542,-3525,-14495,-3517,-14399,-3499,-14304,-3481,-14256,-3472,-14208,-3463,-14113,-3445,-14065,-3435,-13969,-3417,-13922,-3407,-13874,-3398,-13826,-3389,-13779,-3379,-13731,-3370,-13683,-3360,-13635,-3350,-13540,-3331,-13492,-3321,-13396,-3301,-13349,-3291,-13301,-3281,-13253,-3271,-13158,-3251,-13110,-3241,-13015,-3221,-12919,-3200,-12871,-3189,-12824,-3179,-12728,-3158,-12680,-3147,-12633,-3137,-12537,-3115,-12489,-3104,-12441,-3094,-12394,-3083,-12298,-3061,-12250,-3050,-12155,-3028,-12059,-3005,-11964,-2983,-11916,-2972,-11820,-2949,-11725,-2926,-11629,-2903,-11582,-2891,-11486,-2868,-11390,-2844,-11295,-2821,-11199,-2797,-11151,-2785,-11056,-2761,-11008,-2748,-10960,-2736,-10912,-2724,-10865,-2712,-10817,-2699,-10769,-2687,-10721,-2675,-10674,-2662,-10578,-2637,-10482,-2612,-10434,-2600,-10387,-2587,-10339,-2574,-10291,-2562,-10243,-2549,-10195,-2536,-10100,-2510,-10052,-2497,-9908,-2458,-9861,-2445,-9765,-2419,-9717,-2406,-9669,-2393,-9621,-2379,-9526,-2353,-9430,-2326,-9334,-2299,-9239,-2272,-9191,-2258,-9143,-2245,-9047,-2217,-8999,-2204,-8904,-2176,-8808,-2149,-8712,-2121,-8664,-2107,-8617,-2093,-8521,-2065,-8473,-2051,-8377,-2023,-8329,-2009,-8282,-1994,-8138,-1952,-7994,-1909,-7850,-1865,-7803,-1851,-7707,-1822,-7659,-1807,-7563,-1778,-7467,-1749,-7371,-1720,-7276,-1690,-7228,-1675,-7132,-1646,-6988,-1601,-6940,-1586,-6892,-1571,-6844,-1556,-6700,-1511,-6653,-1496,-6509,-1451,-6413,-1421,-6269,-1375,-6173,-1345,-6029,-1299,-5933,-1269,-5790,-1223,-5741,-1207,-5694,-1192,-5646,-1176,-5598,-1161,-5454,-1115,-5310,-1068,-5070,-991,-4926,-944,-4878,-928,-4830,-913,-4686,-866,-4638,-850,-4590,-835,-4350,-756,-4302,-740,-4254,-725,-3821,-583,-3773,-568,-3725,-552,-3677,-536,-3629,-52
db::DPath::pointlist_type pts;
path.transformed (trans).hull (pts);
std::string sres;
for (size_t i = 0; i < pts.size(); ++i) {
if (i > 0) {
sres += ",";
}
sres += db::Point(pts[i]).to_string();
}
EXPECT_EQ (sref, sres);
}
TEST(10)
{
db::Path path;
std::string s ("(0,0;0,1000000000;1000000000,2000000000) w=200000000 bx=50000000 ex=150000000 r=false");
tl::Extractor ex (s.c_str ());
ex.read (path);
// 434400 = (1000+1000*sqrt(2))*200+200*200*(4.0-M_PI)
EXPECT_EQ (tl::to_string (path.area ()), "522842712400000000");
EXPECT_EQ (tl::to_string (path.perimeter ()), "5628427125");
}
TEST(11)
{
db::Path path;
db::Path::pointlist_type pts;
tl::Extractor ("(0,0;1000,0;0,0) w=0 bx=0 ex=0 r=false").read (path);
EXPECT_EQ (tl::to_string (path.box ()), "(0,0;1000,0)");
EXPECT_EQ (tl::to_string (path.perimeter ()), "4000");
EXPECT_EQ (tl::to_string (path.area ()), "0");
pts.clear();
path.hull (pts, 4);
EXPECT_EQ (to_string (pts), "(0,0;1000,0;1000,0;0,0;0,0;1000,0;1000,0;0,0)");
tl::Extractor ("(0,0;1000,0;0,0) w=0 bx=100 ex=0 r=false").read (path);
EXPECT_EQ (tl::to_string (path.box ()), "(-100,0;1000,0)");
EXPECT_EQ (tl::to_string (path.perimeter ()), "4200");
EXPECT_EQ (tl::to_string (path.area ()), "0");
pts.clear();
path.hull (pts, 4);
EXPECT_EQ (to_string (pts), "(-100,0;1000,0;1000,0;0,0;0,0;1000,0;1000,0;-100,0)");
tl::Extractor ("(0,0;1000,0;0,0) w=20 bx=100 ex=0 r=false").read (path);
EXPECT_EQ (tl::to_string (path.box ()), "(-100,-10;1010,10)");
EXPECT_EQ (tl::to_string (path.perimeter ()), "4240");
EXPECT_EQ (tl::to_string (path.area ()), "42000");
pts.clear();
path.hull (pts, 4);
EXPECT_EQ (to_string (pts), "(-100,10;1010,10;1010,-10;0,-10;0,10;1010,10;1010,-10;-100,-10)");
}