// Copyright (c) 2005 Stanford University (USA). // All rights reserved. // // This file is part of CGAL (www.cgal.org); you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; version 2.1 of the License. // See the file LICENSE.LGPL distributed with CGAL. // // Licensees holding a valid commercial license may use this file in // accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ // // // Author(s) : Daniel Russel #ifndef CGAL_KINETIC_SIMULATOR_BASE_H_ #define CGAL_KINETIC_SIMULATOR_BASE_H_ #include #include #include #include #include #include CGAL_KINETIC_BEGIN_NAMESPACE; #ifdef CGAL_KINETIC_CHECK_EXPENSIVE #define CGAL_KINETIC_SIMULATOR_AUDITING //#warning Kinetic data structures will be audited at run time. #endif //! The simulator /*! The simulator is responsible for maintaining the event queue and processing events in the proper order. It takes a PolynomialKernel and an EventQueue as arguments as well as a flag saying whether the computations are performed exactly or not. This flag affects how it treats the output of the solvers. The PolynomialKernel is used to generate solvers as well as evaluate some predicates on the functions being solved. The Simulator creates two types of notifications, one when time is reversed (DIRECTION_OF_TIME) and one when there is a time at which the kinetic data structures can efficient audit themselves (HAS_AUDIT_TIME). The notification is done through a standard interface describe in CGAL::Listener. The solvers defined by the PolynomialKernel should not be the CGAL::Polynomial::GSL_solver or CGAL::Polynomial::Linear_solver. Use CGAL::Kinetic::GSL_solver or CGAL::KDS::Linear_solver instead as they are more robust in the case of kinetic data structures. Even though time can be "reversed" the future is always with increasing time. When time is reversed the current_time() becomes the negative of the old current_time(). In some cases, this means processing some events (since there needs to be a value of time after the last event and before the next one in order to reverse). Auditing occurs once between each pair of disjoint events. It occurs at a time close to the midpoint between the two event times. It is triggered by the time being advanced beyond the midpoint between the two event times. The Root_enumerator type is not that defined by the Polynomial_kernel since the polynomial kernel needs to have a real solver which returns all roots, where as the Simulator solver can check various invariants and skip even roots and things. This is especially important when performing numeric computations. I cannot put the Instantaneous_kernel in here since that might depend on the static data structure being used. */ template */> class Default_simulator: public Ref_counted > { protected: typedef Default_simulator This; //typedef typename Polynomial_kernel_t::Function Poly; struct Listener_core { typedef typename This::Handle Notifier_handle; typedef enum {HAS_AUDIT_TIME, DIRECTION_OF_TIME} Notification_type; }; typedef typename Queue::Priority Queue_time; public: //typedef Kinetic_kernel_t Kinetic_kernel; //! The polynomial kernel. /*! I don't know that there is any actual use for this, so maybe it should not be exposed. */ typedef Polynomial_kernel_t Function_kernel; //! The base class to extend if you want to recieve notifications of events /*! See CGAL::Listener for a description of the notification framework and CGAL/Kinetic/notification_helpers.h for some classes to aid in using notifications. */ typedef Multi_listener Listener; friend class Multi_listener; //! The solver. //typedef typename Function_kernel::Root_stack Root_stack; //! The current time. /*! This is a Root from the Polynomial package and supports whatever operations they support. See CGAL::Polynomial::Simple_interval_root for an example. */ typedef typename Function_kernel::Root Time; //! A key which uniquely identifies a scheduled event /*! If an event is not actually schedule (for example if it occurs past the last time of interest), it is assigned a special Event_key called null_event(). */ //#ifdef NDEBUG typedef typename Queue::Key Event_key; /*#else typedef typename Queue::Key Queue_key; struct Event_key: public Queue_key { Event_key(){} Event_key(Queue_key k): Queue_key(k){} }; #endif*/ //! The basic number type typedef typename Function_kernel::NT NT; //! Create a simulator passing the start time and the end time. Default_simulator(const Time &start_time=Time(0.0), const Time &end_time= internal::infinity_or_max(Time()), Function_kernel fk=Function_kernel()):queue_(start_time, end_time, fk, 100), cur_time_(start_time), last_event_time_(start_time), mp_(fk.rational_between_roots_object()), ir_(fk.is_rational_object()), tr_(fk.to_rational_object()), is_forward_(true) { number_of_events_=0; #ifdef CGAL_KINETIC_SIMULATOR_AUDITING // make it less than the current time. //std::pair ival= to_interval(cur_time_); //audit_time_=NT(ival.first-10.0); audit_time_= to_interval(start_time).first-1; #endif }; //! Return the current time /*! If an event is currently being processed, then this is the failure time of that event. Otherwise it is between the last event which was processed and the next event to be processed. */ Time current_time() const { return cur_time_; } //! Set the current time to t /*! t must be after (or equal to) current_time(). Any events between current_time() and t are processed in the proper order. */ void set_current_time(const Time &t) { CGAL_precondition(t >=cur_time_); /*#ifdef CGAL_KINETIC_CHECK_EXPENSIVE if (current_time() < audit_time_ && t >= audit_time_) { audit_all_kdss(); } #endif*/ while (next_event_time() <= t && !queue_.empty()) { process_next_event(); /*#ifdef CGAL_KINETIC_CHECK_EXPENSIVE if (current_time() < audit_time_ && t >= audit_time_) { audit_all_kdss(); } cur_time_=audit_time_; #endif*/ } cur_time_=(std::min)(t, end_time()); } //! Not clear if I want two methods void set_back_current_time(const Time &t) { CGAL_precondition(t <=cur_time_); cur_time_=t; } //! Return a rational number for current time. /*! This returns a ration number representing the current time. This only returns a valid time if has_rational_current_time() is true. */ NT rational_current_time() const { //CGAL_exactness_precondition_code(CGAL::Interval_nt_advanced ub= CGAL::to_interval(next_event_time())); //CGAL_exactness_precondition(ub.inf() > lb.sup()); // NT ret; if (ir_(cur_time_) && cur_time_ != end_time()) { //if (ir_(cur_time_)) { /*CGAL_KINETIC_ERROR( "rational_current_time() called at time " << current_time() << " which is not rational.\n");*/ return tr_(cur_time_); } else { double ub= to_interval(current_time()).second; if (Time(ub) < next_event_time()) { return NT(ub); } else { //typename Function_kernel::Rational_between_roots bet= kernel_.rational_between_roots_object(); return mp_(last_event_time(), next_event_time()); } } //} } //! Return true if the current time is a rational number. /*! */ bool has_rational_current_time() const { return last_event_time() != next_event_time(); } //! Returns true if the current time is a rational number and there are no events at the current time /*! precondition that has_rational_current_time(). */ bool has_audit_time() const { CGAL_precondition(has_rational_current_time()); return last_event_time() != next_event_time(); } const NT& audit_time() const { CGAL_precondition(has_audit_time()); #ifdef CGAL_KINETIC_SIMULATOR_AUDITING return audit_time_; #else CGAL_precondition(0); static NT z(0); return z; #endif } //! The time of the next event /*! Or end_time() if the queue is empty. */ Time next_event_time() const { if (queue_.empty()) return end_time(); else return Time(queue_.front_priority()); } //! The last time of interest /*! If time is running backwards, then this returns Time::infinity() */ Time end_time() const { if (is_forward_) { return queue_.end_priority(); } else { return std::numeric_limits