View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.openmm;
39  
40  import com.sun.jna.ptr.PointerByReference;
41  import edu.uiowa.jopenmm.OpenMM_Vec3;
42  
43  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_append;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_create;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_destroy;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_get;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_getSize;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_resize;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_set;
50  
51  /**
52   * Vec3 Array.
53   */
54  public class Vec3Array {
55  
56    /**
57     * String vec3 array pointer.
58     */
59    private PointerByReference pointer;
60  
61    /**
62     * OpenMM Vec3 Array constructor.
63     *
64     * @param size The size of the String Array.
65     */
66    public Vec3Array(int size) {
67      pointer = OpenMM_Vec3Array_create(size);
68    }
69  
70    /**
71     * OpenMM Vec3 Array constructor.
72     *
73     * @param pointer The Vec3 Array pointer.
74     */
75    public Vec3Array(PointerByReference pointer) {
76      this.pointer = pointer;
77    }
78  
79    /**
80     * Append a Vec3 to the Vec3Array.
81     *
82     * @param vec3 The Vec3 to append.
83     */
84    public void append(OpenMM_Vec3.ByValue vec3) {
85      OpenMM_Vec3Array_append(pointer, vec3);
86    }
87  
88    /**
89     * Destroy the Vec3Array.
90     */
91    public void destroy() {
92      if (pointer != null) {
93        OpenMM_Vec3Array_destroy(pointer);
94        pointer = null;
95      }
96    }
97  
98    /**
99     * Get a Vec3 from the Vec3Array.
100    *
101    * @return The Vec3 at index i.
102    */
103   public OpenMM_Vec3 get(int i) {
104     return OpenMM_Vec3Array_get(pointer, i);
105   }
106 
107   /**
108    * Convert the Vec3Array to a double array.
109    *
110    * @return The double array.
111    */
112   public double[] getArray() {
113     int size = getSize();
114     double[] array = new double[size * 3];
115     for (int i = 0; i < size; i++) {
116       OpenMM_Vec3 vec3 = get(i);
117       int index = i * 3;
118       array[index] = vec3.x;
119       array[index + 1] = vec3.y;
120       array[index + 2] = vec3.z;
121     }
122     return array;
123   }
124 
125   /**
126    * Get the pointer to the vec3 array pointer.
127    *
128    * @return The pointer to the vec3 array.
129    */
130   public PointerByReference getPointer() {
131     return pointer;
132   }
133 
134   /**
135    * Get the size of the Vec3Array.
136    *
137    * @return The size of the Vec3Array.
138    */
139   public int getSize() {
140     return OpenMM_Vec3Array_getSize(pointer);
141   }
142 
143   /**
144    * Resize the Vec3Array.
145    *
146    * @param size The new size of the Vec3Array.
147    */
148   public void resize(int size) {
149     OpenMM_Vec3Array_resize(pointer, size);
150   }
151 
152   /**
153    * Set a Vec3 in the Vec3Array.
154    *
155    * @param i    The index of the Vec3 to set.
156    * @param vec3 The Vec3 to set.
157    */
158   public void set(int i, OpenMM_Vec3.ByValue vec3) {
159     OpenMM_Vec3Array_set(pointer, i, vec3);
160   }
161 
162   /**
163    * Convert a double array to a Vec3Array.
164    *
165    * @param array The double array.
166    * @return The Vec3Array.
167    */
168   public static Vec3Array toVec3Array(double[] array) {
169     Vec3Array vec3Array = new Vec3Array(0);
170     OpenMM_Vec3.ByValue vec3 = new OpenMM_Vec3.ByValue();
171     for (int i = 0; i < array.length; i += 3) {
172       vec3.x = array[i];
173       vec3.y = array[i + 1];
174       vec3.z = array[i + 2];
175       vec3Array.append(vec3);
176     }
177     return vec3Array;
178   }
179 
180 }