00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef __MMX_RIEMANN_BUNDLE_HPP
00014 #define __MMX_RIEMANN_BUNDLE_HPP
00015 #include <continewz/riemann_surface.hpp>
00016
00017 namespace mmx {
00018 #define TMPL_DEF template<typename Germ,typename R,typename V= std_riemann>
00019 #define TMPL template<typename Germ,typename R,typename V>
00020 #define C complex<R>
00021 #define Square riemann_square<R,V>
00022 #define Point riemann_point<R,V>
00023 #define Squares table<bool,Square >
00024 #define Iterator iterator<Square >
00025 #define Surface riemann_surface<R,V>
00026 #define Bundle riemann_bundle<Germ,R,V>
00027 #define Bundle_rep riemann_bundle_rep<Germ,R,V>
00028 #define Table table<vector<Germ>,Square >
00029
00030
00031
00032
00033
00034 template<typename P, typename Germ> bool inside (const P& p, const Germ& g);
00035
00036 template<typename P, typename Germ> inline bool
00037 better (const P& p, const Germ& g1, const Germ& g2) {
00038 return inside (p, g1);
00039 }
00040
00041 TMPL bool
00042 inside (const Square& s, const Germ& g) {
00043
00044 R x= Re (center (s));
00045 R y= Im (center (s));
00046 R r= radius (s);
00047 return
00048 inside (C (x-r, y-r), g) &&
00049 inside (C (x-r, y+r), g) &&
00050 inside (C (x+r, y-r), g) &&
00051 inside (C (x+r, y+r), g);
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 TMPL_DEF
00061 class riemann_bundle_rep REP_STRUCT {
00062 public:
00063 Surface surface;
00064 Table bundle;
00065 public:
00066 inline riemann_bundle_rep () {}
00067 inline riemann_bundle_rep (const format<R>& fm):
00068 surface (fm), bundle (format<vector<Germ > > (format<Germ > (fm)),
00069 format<Square > (fm)) {}
00070 inline riemann_bundle_rep (const Surface& rs):
00071 surface (rs), bundle (vector<Germ > (format<Germ > (CF(rs))),
00072 format<Square > (CF(rs))) {}
00073 inline riemann_bundle_rep (const Surface& rs, const Table& bun):
00074 surface (rs), bundle (bun) {}
00075 void upgrade (const Squares& ss);
00076 };
00077
00078 TMPL_DEF
00079 class riemann_bundle {
00080 INDIRECT_PROTO_3 (riemann_bundle, riemann_bundle_rep, Germ, R, V)
00081 public:
00082 inline riemann_bundle ():
00083 rep (new Bundle_rep ()) {}
00084 inline riemann_bundle (const format<R>& fm):
00085 rep (new Bundle_rep (fm)) {}
00086 inline riemann_bundle (const Surface& rs):
00087 rep (new Bundle_rep (rs)) {}
00088 riemann_bundle (const Surface& rs, const Germ& g):
00089 rep (new Bundle_rep (rs))
00090 {
00091 for (Iterator it= entries (rs->squares); busy (it); ++it)
00092 rep->bundle[*it]= vec<Germ> (g);
00093 }
00094 riemann_bundle (const Surface& rs, const Table& bun):
00095 rep (new Bundle_rep (rs, bun)) {}
00096 };
00097 INDIRECT_IMPL_3 (riemann_bundle, riemann_bundle_rep,
00098 typename Germ, Germ, typename R, R, typename V, V)
00099 DEFINE_UNARY_FORMAT_3_bis (riemann_bundle)
00100
00101 TMPL inline format<R> CF (const Bundle& b) {
00102 return CF (b->surface); }
00103
00104
00105
00106
00107
00108 template<typename Op, typename Germ, typename R, typename V> nat
00109 unary_hash (const Bundle& b) {
00110 return hard_hash (b);
00111 }
00112
00113 template<typename Op, typename Germ, typename R, typename V> bool
00114 binary_test (const Bundle& b1, const Bundle& b2) {
00115 return hard_eq (b1, b2);
00116 }
00117
00118 TRUE_IDENTITY_OP_SUGAR(TMPL,Bundle)
00119 EXACT_IDENTITY_OP_SUGAR(TMPL,Bundle)
00120
00121 TMPL syntactic
00122 flatten (const Bundle& b) {
00123 return apply ("bundle", flatten (b->surface), flatten (b->bundle));
00124 }
00125
00126 TMPL
00127 struct binary_helper<Bundle >: public void_binary_helper<Bundle > {
00128 static inline string short_type_name () {
00129 return "Rbun" * Short_type_name (Germ) * Short_type_name (R); }
00130 static inline generic full_type_name () {
00131 return gen ("Riemann_bundle", Full_type_name (R), Full_type_name (Germ)); }
00132 static inline generic disassemble (const Bundle& b) {
00133 return gen_vec (as<generic> (b->surface), as<generic> (b->bundle)); }
00134 static inline Bundle assemble (const generic& v) {
00135 return Bundle (as<Surface > (vector_access (v, 0)),
00136 as<Table > (vector_access (v, 1))); }
00137 static inline void write (const port& out, const Bundle& b) {
00138 binary_write<Surface > (out, b->surface);
00139 binary_write<Table > (out, b->bundle); }
00140 static inline Bundle read (const port& in) {
00141 Surface rs= binary_read<Surface > (in);
00142 Table bun= binary_read<Table > (in);
00143 return Bundle (rs, bun); }
00144 };
00145
00146
00147
00148
00149
00150 TMPL vector<Germ >
00151 merge (const Square& s, const vector<Germ >& v1, const vector<Germ >& v2) {
00152 if (N(v1) == 0) return v2;
00153 if (N(v2) == 0) return v1;
00154 if (N(v1) == 1 && inside (s, v1[0])) return v1;
00155 if (N(v2) == 1 && inside (s, v2[0])) return v2;
00156 vector<Germ > r= v1;
00157 nat i, j;
00158 for (i=0; i<N(v2); i++) {
00159 for (j=0; j<N(r); j++)
00160 if (v2[i] == r[j]) break;
00161 if (j == N(r)) r << v2[i];
00162 }
00163 return r;
00164 }
00165
00166 TMPL void
00167 Bundle_rep::upgrade (const Squares& ss) {
00168 for (Iterator it= entries (ss); busy (it); ++it) {
00169 Square s1= *it;
00170 Square s2= inside (surface)->upgrade (s1);
00171 vector<Germ> v1= bundle[s1];
00172 vector<Germ> v2= bundle[s2];
00173 reset (bundle, s1);
00174 bundle[s2]= merge (s2, v1, v2);
00175 }
00176 }
00177
00178
00179
00180
00181
00182 TMPL Squares
00183 glue (Bundle& b1, const Point& p1, const Bundle& b2, const Point& p2) {
00184 Squares replaced= glue (inside (b1)->surface, p1, b2->surface, p2);
00185 for (Iterator it= entries (b2->bundle); busy (it); ++it)
00186 inside (b1)->bundle[*it]= merge (*it, b1->bundle[*it], b2->bundle[*it]);
00187 inside (b1)->upgrade (replaced);
00188 return replaced;
00189 }
00190
00191 TMPL void
00192 glue (Bundle& b1, const Point& p1, const Bundle& b2) {
00193 Point p2= lift (b2->surface, project (p1));
00194 glue (b1, p1, b2, p2);
00195 }
00196
00197 TMPL inline vector<Germ >
00198 germs (const Bundle& b, const Point& p_old) {
00199 Point p= upgrade (b->surface, p_old);
00200 return b->bundle[owner (p)];
00201 }
00202
00203 TMPL Germ
00204 germ (const Bundle& b, const Point& p_old) {
00205 Point p= upgrade (b->surface, p_old);
00206 vector<Germ > v= germs (b, p);
00207 nat best= N(v);
00208 for (nat i=0; i<N(v); i++)
00209 if (inside (project (p), v[i]))
00210 if (best == N(v) || better (project (p), v[i], v[best]))
00211 best= i;
00212 if (best != N(v)) return v[best];
00213 mmout << "p= " << p << "\n";
00214 mmout << "v= " << v << "\n";
00215 ERROR ("germ not found");
00216 return v[0];
00217 }
00218
00219 TMPL inline Point lift (const Bundle& b, const C& z) {
00220 return lift (b->surface, z); }
00221 TMPL inline vector<Point > lifts (const Bundle& b, const C& z) {
00222 return lifts (b->surface, z); }
00223 TMPL inline Point move (const Bundle& b, const Point& p, const C& delta) {
00224 return move (b->surface, p, delta); }
00225
00226 TMPL inline Surface bundle_surface (const Bundle& b) {
00227 return b->surface; }
00228
00229 #undef TMPL_DEF
00230 #undef TMPL
00231 #undef C
00232 #undef Square
00233 #undef Point
00234 #undef Squares
00235 #undef Iterator
00236 #undef Surface
00237 #undef Bundle
00238 #undef Bundle_rep
00239 #undef Table
00240 }
00241 #endif // __MMX_RIEMANN_HPP